Using for loop to load multiple mat files of different names

44 次查看(过去 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
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
编辑: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
Peram Balakrishna 2021-11-16
It worked perfectly well, but can you explain the code for understanding.

请先登录,再进行评论。


Ray
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
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
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
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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by