Using a loop to load multiple mat files from measurement into the workspace
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone,
it is my first time here.
My goal is to load a specific information out of around 20188 mat-files. Each file is a measurement-point. Each file includes two types of Informations. It is the surface and width of a area. I m only interested in the width. In the first picture you can see a Overview over the first five mat-files as the first five measurement points.The second picture here shows the structure of the first of the 20188 mat-files. In the picture the germen language is used. (width=max_Breite and surface=Flaeche, Measurement=Messung)


My code is here. The programm should write the parameter max_Breite in the array width. But because of the Ending .max_Breite he can't find the file.
width=[0];
for i=1:20188
switch i
case i<=9
width(i)=load(sprintf('Messung_0000%d.mat.max_Breite',i));
case i>=10 && i<100
width(i)=load(sprintf('Messung_000%d.mat.max_Breite',i));
case i>=100 && i<1000
width(i)=load(sprintf('Messung_00%d.mat.max_Breite',i));
case i>=1000 && i<10000
width(i)=load(sprintf('Messung_0%d.mat.max_Breite',i));
otherwise
width(i)=load(sprintf('Messung_%d.mat.max_Breite',i));
end
end
Error:
Error using load
Unable to read file 'Messung_00001.mat.max_Breite'. No such file or directory.
Error in Daten (line 22)
width(i)=load(sprintf('Messung_0000%d.mat.max_Breite',i));
Does somebody have a idear. I appreciate every kind of help and i m thankfull for everything. If you want you can write in german.
I thank you in advance
Erik
2 Kommentare
Stephen23
am 3 Sep. 2022
Bearbeitet: Stephen23
am 3 Sep. 2022
"But because of the Ending .max_Breite he can't find the file."
I don't see anything in the LOAD documentation that allows the filenames to be suffixed with names of variables inside the MAT file. Please show a link to the MATLAB documentation, which supports your attempted syntax.
If these are valid MAT files then LOADs output will be a scalar structure containing all of the imported data: why did you preallocate that array (misleadingly named WIDTH) with a scalar numeric?
Note that you can get rid of that complex SWITCH by simply specifying the fieldwidth in the SPRINTF format:
sprintf('Messung_%05d.mat',5)
sprintf('Messung_%05d.mat',543)
sprintf('Messung_%05d.mat',54321)
Antworten (1)
Stephen23
am 3 Sep. 2022
Bearbeitet: Stephen23
am 3 Sep. 2022
P = '.'; % absolute or relative path to where the files are saved
S = dir(fullfile(P,'Messung_*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F);
S(k).width = D.max_Breite;
end
all_widths = vertcat(S.width)
The filenames and MAX_BREITE are stored in the structure array S. For example, the second file:
S(2).name
S(2).width
0 Kommentare
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!