Loading files by replacing some part of the name with a variable

조회 수: 2 (최근 30일)
Simon Schirk
Simon Schirk 2023년 12월 4일
댓글: Matt J 2023년 12월 4일
Hi, I want to load a bunch of files in my workspace. There all name simmilarly and there's a number counting up, but its not just at the end of the Filename, so just replacing it with 'i' doesn't work.
Any ideas?
The 'i' in the file name are two digits. Starting with '01' and ending with '41'
I found a Video of someone explaining a similar issue, but I wasn't able to adapt the solution to my code.
for i = 01:41
FileName = 'GNSS_Validierung_0i.mat';
FolderName = 'C:\Users\Admin\Documents\BA\MATLAB\Routenplanung\versuchsdaten_maxi';
File = fullfile(FolderName, FileName);
load(File);
x_DIY_flip = GNSS_Validierung_0i.Y(23).Data';
y_DIY_flip = GNSS_Validierung_0i.Y(25).Data';
x_ppm_flip = GNSS_Validierung_0i.Y(24).Data';
y_ppm_flip = GNSS_Validierung_0i.Y(26).Data';
.
.
.
end
  댓글 수: 2
Stephen23
Stephen23 2023년 12월 4일
편집: Stephen23 2023년 12월 4일
The MATLAB documentation shows you one approach using SPRINTF:
And then remember to always LOAD into an output variable:
S = load(..);
and then access its fields. Then you can avoid that major problem with magical dynamic variable names.
Dyuman Joshi
Dyuman Joshi 2023년 12월 4일
How do you get the data i.e. all the mat file, in the manner (using numbers in the name i.e. dynamically naming them) you have now? Is the data output from some code?

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2023년 12월 4일
편집: Matt J 2023년 12월 4일
Pre-generate all of the filenames and loop over them.
FileNames = compose("GNSS_Validierung_%.2d",(1:41)' )
FileNames = 41×1 string array
"GNSS_Validierung_01" "GNSS_Validierung_02" "GNSS_Validierung_03" "GNSS_Validierung_04" "GNSS_Validierung_05" "GNSS_Validierung_06" "GNSS_Validierung_07" "GNSS_Validierung_08" "GNSS_Validierung_09" "GNSS_Validierung_10" "GNSS_Validierung_11" "GNSS_Validierung_12" "GNSS_Validierung_13" "GNSS_Validierung_14" "GNSS_Validierung_15" "GNSS_Validierung_16" "GNSS_Validierung_17" "GNSS_Validierung_18" "GNSS_Validierung_19" "GNSS_Validierung_20" "GNSS_Validierung_21" "GNSS_Validierung_22" "GNSS_Validierung_23" "GNSS_Validierung_24" "GNSS_Validierung_25" "GNSS_Validierung_26" "GNSS_Validierung_27" "GNSS_Validierung_28" "GNSS_Validierung_29" "GNSS_Validierung_30"
  댓글 수: 6
Stephen23
Stephen23 2023년 12월 4일
편집: Stephen23 2023년 12월 4일
@Matt J: ah, I missed that dot indexing. Note that ".mat" is not valid in fieldnames.
Matt J
Matt J 2023년 12월 4일
@Stephen23 Good point. I've removed the .mat sub-string from the code I proposed (it was never necessary).

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2023년 12월 4일
편집: Stephen23 2023년 12월 4일
Do NOT name each variable dynamically. Unless you want to force yourself into writing slow, complex, inefficient, insecure, obfuscated code that is buggy and hard to debug.
Always LOAD into an output variable (which is a scalar structure). The your data-access problems are easily avoided:
P = 'C:\Users\Admin\Documents\BA\MATLAB\Routenplanung\versuchsdaten_maxi';
for k = 01:41
F = sprintf('GNSS_Validierung_%02d.mat',k);
C = struct2cell(load(fullfile(P,F));
assert(isscalar(C),'Exactly one variable is allowed!')
x_DIY_flip = C{1}.Y(23).Data.';
y_DIY_flip = C{1}.Y(25).Data.';
x_ppm_flip = C{1}.Y(24).Data.';
y_ppm_flip = C{1}.Y(26).Data.';
.
.
.
end
Note that your code would be simpler, more efficient, and much more robust if the variable names were exactly the same in every MAT file. That is to say, the data you have been given is badly designed.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by