필터 지우기
필터 지우기

load different data types from the same subfolder simultaneously

조회 수: 2 (최근 30일)
Anastasiia Khibovska
Anastasiia Khibovska 2023년 3월 7일
답변: Voss 2023년 3월 7일
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
Anastasiia Khibovska 2023년 3월 7일
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