How to load MAt file into 3D matrix

I want to load 516 Mat files(240x320)into a 3D Matrix A(240,320,516)
The files are called PVCWARM00071,....,PVCWARM0007516
my code so far:
A = zeros(240,320,516)
nummer=0
>> for i= 1:516
nummer = nummer +1
ext = '.MAT'
naam = 'PVCWARM0007'
file= strcat(naam,num2str(nummer),ext)
A(:,:,i) = load(file)
end
i get the error:Conversion to double from struct is not possible. i already tried to make some adjustments with struct2cell and cell2mat without result.
Can someone help?
Thanks

2 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Ag. de 2013
What is the name of your matrix in you mat file?
Maxim De Roeck
Maxim De Roeck el 9 de Ag. de 2013
the 3d matrix is called A, the Mat files are 240x320 arrays without a name

Iniciar sesión para comentar.

 Respuesta aceptada

Evan
Evan el 9 de Ag. de 2013
Editada: Evan el 9 de Ag. de 2013
The answer can be found in the documentation of the load function:
S = load(FILENAME) loads the variables from a MAT-file into a structure array, or data from an ASCII file into a double-precision array.
Since you already initialize A as a double, the line A(:,:i) = load(file) is attempting to save load's structure output into your 3D matrix.
If you already know the name of your variables as they are saved in your mat file, you can call load without an output argument to put them in the workspace, then save them to your 3D matrix. Again, though, it helps to know or find out the variable names when doing this:
A = zeros(240,320,516);
nummer = 0;
for i = 1:516
nummer = nummer +1
ext = '.MAT';
naam = 'PVCWARM0007';
file = strcat(naam,num2str(nummer),ext);
load(file)
A(:,:,i) = B; %if, for example, the variable's name is B
end

7 comentarios

Maxim De Roeck
Maxim De Roeck el 9 de Ag. de 2013
i understand, but my M-files consists of an 240x320 array filled with values without a name.
Evan
Evan el 9 de Ag. de 2013
Editada: Evan el 9 de Ag. de 2013
What returns when you type the following?
whos -file filename %where filename is the name of your .mat file
Maxim De Roeck
Maxim De Roeck el 9 de Ag. de 2013
so you say that i should make a variable for each file and insert it like that?
Maxim De Roeck
Maxim De Roeck el 9 de Ag. de 2013
Editada: Maxim De Roeck el 9 de Ag. de 2013
>> whos -file PVCWARM00071.MAT Name Size Bytes Class Attributes
PVCWARM00071 240x320 614400 double
PVCWARM00071_DateTime 1x7 56 double
PVCWARM00071_FrameInfo 1x2 16 double
PVCWARM00071_ObjectParam 1x10 80 double
PVCWARM00071_Scaling 1x9 72 double
Evan
Evan el 9 de Ag. de 2013
Editada: Evan el 9 de Ag. de 2013
Okay, so it looks like your variable names are the same as your file names. This means you can just access the arrays using the "file" strings you've already created. Since it looks like each file is going to have a differently named variable, I would return to specifying an output for your load command and just do the following:
A = zeros(240,320,516);
nummer = 0;
for i = 1:516
nummer = nummer +1
ext = '.MAT';
naam = 'PVCWARM0007';
file = strcat(naam,num2str(nummer),ext);
S = load(file);
A(:,:,i) = S.(file);
end
Does that work?
i fixed it:
for i = 1:516
eval(['load PVCWARM0007',int2str(i),';']);
eval (['A(:,:,i) = PVCWARM0007',int2str(i),';']);
end
thanks anyway!!
Evan
Evan el 9 de Ag. de 2013
Editada: Evan el 9 de Ag. de 2013
The method you're using is not recommended, which is why I suggested using dynamic field references. See Azzi's link for an explanation of why eval should be avoided. You also might find this page helpful.

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Ag. de 2013
Editada: Azzi Abdelmalek el 9 de Ag. de 2013
A = zeros(240,320,516)
nummer=0
for i= 1:516
nummer = nummer +1
ext = '.MAT'
naam = 'PVCWARM0007'
file= strcat(naam,num2str(nummer),ext)
variable_name=whos('-file',file);
x=load(file)
A(:,:,i) = x.(variable_name.name)
end

2 comentarios

Maxim De Roeck
Maxim De Roeck el 9 de Ag. de 2013
Editada: Azzi Abdelmalek el 9 de Ag. de 2013
i fixed it:
for i = 1:516
eval(['load PVCWARM0007',int2str(i),';']);
eval (['A(:,:,i) = PVCWARM0007',int2str(i),';']);
end
thanks anyway!!

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by