Using for loop to load multiple mat files of different names
조회 수: 25 (최근 30일)
이전 댓글 표시
Hi guys, I'm new to matlab, I'm trying to load multiple mat files from a folder, I had googled for a few ways to solve this question, but none of them solve mine. My .mat file are all different name, such like 2011_003260, 2011_003255 and bla bla bla, as you can see, it is not a continuous number, and not a string, so, how can i loop it with a for loop? Besides of that, I would like to save the information in my .mat file into a table, is there any way I can do it?
files = dir('*.mat'); %try to look for all the .mat files under the folder
for i=1:length(files)
load([files '.mat']); %load the files
SBDInfo(TotalObjIndex).ImageName = ImgName; %Save the information into the 'variables- files'/ 'field'
end
댓글 수: 1
Umaisa
2022년 10월 20일
can you please tell me how can i load all the audio files (.wav) into matlab .i tried using different methods and loops but nothing is working
답변 (3개)
Konstantinos Sofos
2015년 6월 2일
편집: Konstantinos Sofos
2015년 6월 2일
Hi Jonathan,
You are quite close. If you want to process all the files whose name matches a pattern in a directory, you can use DIR to select the files.
myFolder = 'C:\Documents and Settings\MATLAB\'; % Define your working folder
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData(k) = load(fullFileName);
end
Regards,
댓글 수: 9
Peram Balakrishna
2021년 11월 16일
It worked perfectly well, but can you explain the code for understanding.
Ray
2015년 9월 13일
편집: Walter Roberson
2015년 9월 13일
Hello Konstantinos Sofos,
Your code seems to be working as far reading the files, however after reading several files, i get an error indicating
Subscripted assignment between dissimilar structures.
Error in scrap (line 62)
matData(k) = load(fullFileName);s
The code is
myFolder = '/Users/redhwanmawari/Documents/MATLAB/2DimenstionalTestingGT20150907/LOS0dbm15ft_finalData'; % Define your working folder
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData(k) = load(fullFileName);
end
Please advise
댓글 수: 3
Elisabetta
2023년 5월 21일
Hey guys, I'm new to matlab and I used Ray's code edited by Water Roberson - thank you so much guys, it worked. The structure resulted from this code contains 2 fields: "Dataset" (containing the whole path of each object) and "data" containing the actual data enclosed in each object. I'm trying to replace the whole path in "Dataset" with just the name of each object, but I'm struggling.
Example: I would like to replace '/data/RAW/EEG_LAPTOP/Raw Files/eegfile1.eeg' (contained in the first row of the "Dataset" field, with just "eegfile1". And I'm trying to do this for all the files, "eegfile2", "eegfile3" and so on. Any suggestion would be most welcome.
Thanks!
Walter Roberson
2023년 5월 21일
[~, YourStruct(INDEX).Dataset] = fileparts(YourStruct(INDEX).Dataset);
You can vectorize to do the whole struct at the same time:
[~, temp] = fileparts({YourStruct.Dataset});
[YourStruct.Dataset] = deal(temp{:});
Ray
2015년 9월 14일
편집: Walter Roberson
2017년 9월 12일
Thanks that took care of the error that i had. Now i am trying to convert all the matrices from 18 by 18 matrices in each file into one single column i.e 324 by 1 column. After that once all the files contain one single column, i need to combine all the files into one single column. The code is as follows however i keep getting this error:Argument to dynamic structure reference must evaluate to a valid field name.
Error in scrap (line 70)
rawdata.(reshapedData{p}) = reshape(rawdata.(matData{j}),324,1);
Below is the code i wrote:
p=1;
for j=1:length(matFiles)
rawdata.(reshapedData{p}) = reshape(rawdata.(matData{j}),324,1);
p=p+324;
end
% % Concatenate data
for k = 1:length(matfiles)
rawdata.(matData{k}) = [rawdata.(matData{k});rawdata.(matData{k})];
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!