- Set the `directory` variable to the path where your `.mat` files are located.
- Use `dir` to get a list of all files with a `.mat` extension in the specified directory.
- Initialize empty arrays (`column1`, `column2`, `column3`, `column4`) to store the concatenated data from each column.
- Construct the full path for each file using `fullfile`. Load each file using the `load` function.
- Extract the matrix (assumed to be named `dataMatrix` here) and concatenate each column vertically using the `;` operator.
I have a set of .mat files with random names. I want to import all those files from the directory using loop. Each file has 4 columns and I want to vertically concatenate each column from all the imported files. Please help with the code.
2 views (last 30 days)
Show older comments
I have a set of .mat files with random names. I want to import all those files from the directory using loop. Each file has 4 columns and I want to vertically concatenate each column from all the imported files. Please help with the code.
0 Comments
Answers (1)
Avni Agrawal
on 10 Sep 2024
I understand that you are trying to import all `.mat` files from a directory and concatenate their columns vertically, you can use MATLAB's `dir` function to list all the files, and then loop through each file to load and concatenate the data.
Here's a step-by-step guide with example code:
% Specify the directory containing the .mat files
directory = 'your_directory_path';
% Get a list of all .mat files in the directory
fileList = dir(fullfile(directory, '*.mat'));
% Initialize cell arrays to store concatenated columns
column1 = [];
column2 = [];
column3 = [];
column4 = [];
% Loop through each file
for i = 1:length(fileList)
% Construct the full file path
filePath = fullfile(directory, fileList(i).name);
data = load(filePath);
% Assuming the data is stored in a variable named 'dataMatrix'
dataMatrix = data.dataMatrix;
% Concatenate each column vertically
column1 = [column1; dataMatrix(:, 1)];
column2 = [column2; dataMatrix(:, 2)];
column3 = [column3; dataMatrix(:, 3)];
column4 = [column4; dataMatrix(:, 4)];
end
%Display the concatenated columns (optional)
disp('Concatenated Column 1:');
disp(column1);
disp('Concatenated Column 2:');
disp(column2);
disp('Concatenated Column 3:');
disp(column3);
disp('Concatenated Column 4:');
disp(column4);
I hope this helps!
1 Comment
Walter Roberson
on 11 Sep 2024
Edited: Walter Roberson
on 11 Sep 2024
More efficiently:
% Specify the directory containing the .mat files
directory = 'your_directory_path';
% Get a list of all .mat files in the directory
fileList = dir(fullfile(directory, '*.mat'));
numfiles = length(fileList);
datacell = cell(numfiles, 1);
% Loop through each file
for i = 1:length(fileList)
% Construct the full file path
filePath = fullfile(directory, fileList(i).name);
data = load(filePath);
% Assuming the data is stored in a variable named 'dataMatrix'
datacell{i} = data.dataMatrix(:,1:4);
end
dataMatrix = cell2mat(datacell);
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!