Load data with varying names

조회 수: 2 (최근 30일)
Jeroen van Bemmel
Jeroen van Bemmel 2022년 2월 1일
편집: Jeroen van Bemmel 2022년 2월 1일
I have data files (.mat format) which consists of 140 files which only vary in their name which a increasing number:
cam_0_C182_0000001.mat
cam_0_C182_0000002.mat
...
cam_0_C182_0000140.mat
From those files (which are matlab structures, and i am intrested in a single value from a matrix within that structure.
How can i construct a loop which automatically loads all the files, and gets that one value, and places them in a new array?
for i = 1:140
Structure = cam_0_C182_0000('i').mat;
Output(i) = Structure.X(6,73); % get value (6,73) from matrix X, within the structure
end
My problem mainly is that i am not sure how to convert the numbers i from the loop to string value for the .m files in the folder, furthermore does 1 have to be translated to string (001), 45 to (045) and 101 to (101), so how do i format this number correctly to place zeros in front of the number if lover then 10, or then 100?
Thanks in advance
  댓글 수: 1
Stephen23
Stephen23 2022년 2월 1일
편집: Stephen23 2022년 2월 1일
Your description is unclear: "I have data files (.m format) which consists of 140 files which only vary in their name which a increasing number: cam_0_C182_0000001.mat...". So what do you actually have saved in that folder:
  • M-files (.m, as you wrote in your description) which are text files containing code.
  • Mat files (.mat, as your examples show) which are binary files containing data.
Mixing things up makes it hard for us to know what you are actually doing.

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

채택된 답변

Stephen23
Stephen23 2022년 2월 1일
편집: Stephen23 2022년 2월 1일
The MATLAB documentation explains how:
The easy approach is to use DIR:
P = 'absolute or relative path to where the files are saved':
S = dir(fullfile(P,'cam_0_C182_0*.mat'));
N = numel(S);
V = nan(1,N);
for k = 1:N
D = load(fullfile(P,S(k).name));
V(k) = D.X(6,73);
end
Else use SPRINTF if you really want to generate the filenames from numerics:
N = 140;
V = nan(1,N);
for k = 1:N;
F = sprintf('cam_0_C182_%07d.mat',k);
D = load(fullfile(P,F));
V(k) = D.X(6,73);
end
In both cases you will need to LOAD the filedata (the syntax you show in your question does not exist).
  댓글 수: 1
Jeroen van Bemmel
Jeroen van Bemmel 2022년 2월 1일
Thank you so much!
much less code then i was anticipating.
you are the mvp ;)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by