Using for loop to load multiple mat files of different names

115 ビュー (過去 30 日間)
Jonathan Lee
Jonathan Lee 2015 年 6 月 2 日
コメント済み: Walter Roberson 2023 年 5 月 21 日
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 日
編集済み: 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 件のコメント
preksha pareek
preksha pareek 2017 年 9 月 12 日
Thank you so much for help now I am able to access the file.
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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by