load different data types from the same subfolder simultaneously

2 次查看(过去 30 天)
Hi everyone!
I am not sure if it is possible to do in matlab, and I searched a lot of places for an answer, so I thought might take a shot at asking a question myself!
I have a folder with multiple subfolders. Each subfolder contains data petraining to each individual subject. In this folder, the data comes from two types of files: .mat and .dat:
I am able to extract and process and save data from each type of files separately using this code as an example:
% Specify the folder where the files live.
myFolder = '/Users/Documents/MGS';
filePattern = fullfile(myFolder, '**/*.mat'); % or .dat separately
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
load(fullFileName);
% cut the actual processing code for space
end
fname = baseFileName(1:end-4);
save(sprintf('%s.mat',fname), 'edf');
end
But I want to load two file types simuntanously in a loop (making sure two files come from one subfolder), extract certain variables from both and save the result. Is it possible in matlab?
Thank you in advance!
  2 个评论
Walter Roberson
Walter Roberson 2023-3-7
Is there only ever one .mat file per folder? Is the file name of the .dat file always the same? What is the structure of the .dat file? Which directory should the data be saved into ?
Anastasiia Khibovska
yup, there is always one .mat file and .day file is a table array. .dat fiile name has the same string characters except numbers and out directory can be either in one folder or in the same subfolders!

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2023-3-7
If there is one .mat and one .dat file in each folder, then this should work to get the names (full path names) of each .mat and the corresponding .dat in the same folder.
myFolder = '/Users/Documents/MGS';
matFilePattern = fullfile(myFolder, '**', '*.mat');
datFilePattern = fullfile(myFolder, '**', '*.dat');
matFiles = dir(matFilePattern);
datFiles = dir(datFilePattern);
[ism,idx] = ismember({matFiles.folder},{datFiles.folder});
matFileNames = fullfile({matFiles(ism).folder}, {matFiles(ism).name});
datFileNames = fullfile({datFiles(idx(ism)).folder}, {datFiles(idx(ism)).name});
for ii = 1:numel(matFileNames)
% do stuff with matFileNames{ii} and datFileNames{ii}
end
Of course you still have to write code to read the .dat file(s) if you don't have that already.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by