Effacer les filtres
Effacer les filtres

Load data with varying names

3 vues (au cours des 30 derniers jours)
Jeroen van Bemmel
Jeroen van Bemmel le 1 Fév 2022
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 commentaire
Stephen23
Stephen23 le 1 Fév 2022
Modifié(e) : Stephen23 le 1 Fév 2022
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.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 1 Fév 2022
Modifié(e) : Stephen23 le 1 Fév 2022
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 commentaire
Jeroen van Bemmel
Jeroen van Bemmel le 1 Fév 2022
Thank you so much!
much less code then i was anticipating.
you are the mvp ;)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by