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.

3 次查看(过去 30 天)
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.

回答(1 个)

Avni Agrawal
Avni Agrawal 2024-9-10
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);
  • 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 hope this helps!
  1 个评论
Walter Roberson
Walter Roberson 2024-9-11
编辑:Walter Roberson 2024-9-11
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Management 的更多信息

产品


版本

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by