Load data with varying names

2 visualizaciones (últimos 30 días)
Jeroen van Bemmel
Jeroen van Bemmel el 1 de Feb. de 2022
Editada: Jeroen van Bemmel el 1 de Feb. de 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 comentario
Stephen23
Stephen23 el 1 de Feb. de 2022
Editada: Stephen23 el 1 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 1 de Feb. de 2022
Editada: Stephen23 el 1 de Feb. de 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 comentario
Jeroen van Bemmel
Jeroen van Bemmel el 1 de Feb. de 2022
Thank you so much!
much less code then i was anticipating.
you are the mvp ;)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by