Load data with varying names

1 次查看(过去 30 天)
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
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