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 (최근 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에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by