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 Center および File ExchangeStructures についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by