Strange problem - vector dimensions

1 visualización (últimos 30 días)
Kyriacos
Kyriacos el 21 de Abr. de 2012
Hello, I am facing a weird problem and I do not know why. I have saved on my disk some .mat files called: SIMGAtheta1_0 SIMGAtheta2_0 ... SIMGAtheta21_0
Then, I am trying to run the following code:
for kk=1:21;
disp(kk)
s = (['SIMGAtheta', num2str(kk), '_', num2str(q)]);
load(s)
A(kk) = ... calculations.... ;
clear SIMGA*
end
That is, I want it to upload each SIMGA... file in turn, perform some calculations, save those as the next (kk-th) element of the vector A and again... 21 times. The problem is that when kk=7, it puts the new calculation as a first element in the vector (!). So it messes up the whole thing. And I do not see why this is going on, and why at the 7th loop. Note that if I break the latter into two, it works. I.e. if I do the following I get what I want:
for kk=1:6;
disp(kk)
s = (['SIMGAtheta', num2str(kk), '_', num2str(q)]);
load(s)
A1(kk) = ... calculations...;
clear SIMGA*
end
for kk=7:21;
disp(kk)
s = (['SIMGAtheta', num2str(kk), '_', num2str(q)]);
load(s)
A2 = ....calculations....;
clear SIMGA*
end
A = [A1 A2];
But I do not understand why it does not work doing it in "one go". Thanks for any help/suggestion.
Kyriacos
[EDITED, Jan Simon, code formatted]

Respuesta aceptada

Richard Brown
Richard Brown el 21 de Abr. de 2012
My guess is that there is a variable called 'A' in your 7th m-file. What if you call your array AAA (to avoid naming conflicts) instead, does this fix it immediately?
This would also explain why your second example works, with variables called A1 and A2.
  3 comentarios
Richard Brown
Richard Brown el 21 de Abr. de 2012
Ok, is there a variable called 'kk' in it?
Daniel Shub
Daniel Shub el 23 de Abr. de 2012
+1 for your comment about kk.

Iniciar sesión para comentar.

Más respuestas (3)

Jan
Jan el 21 de Abr. de 2012
Using load without catching the input in a variable is prone to errors. Please try:
Data = load(s)
and perform the calculations with the fields of Data. This avoids interferences between your program and the contents of the MAT files.
  1 comentario
Richard Brown
Richard Brown el 21 de Abr. de 2012
Haha, I was going to append my answer with: 'I'm sure someone will come along soon and give you a lecture about using load with no output arguments' :-)

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 21 de Abr. de 2012
I don't see why the second chunk of code would work. A2 is just a scalar (it doesn't depend on kk), so A would be a 1x7 vector, not a 1x21 vector. So I'm really puzzled why the second code works.
Here is some more robust code for retrieving variables from a mat file:
% Load the mat file into a local structure variable.
storedStructure= load(matFileName);
% Print out all the fieldnames to the command window
% so we can see what they are:
fieldnames(storedStructure)
% See if we can pull out the myVariable1 variable.
hasField = isfield(storedStructure, 'myVariable1');
if hasField
% Variable myVariable1 is in the mat file.
% Assign the myVariable1 field of the structure to a local variable.
myVariable1= storedStructure.myVariable1;
else
% mat file exists, but the variable(s) aren't inside.
message = sprintf('The variable myVariable1 does not exist in mat file:\n%s', matFileName);
msgboxw(message);
return; % or break, to bail out of loop, or function.
end
Anyway, just put a breakpoint on the line
A(kk) = ....
and when it stops there, at the 7th iteration, tell us what kk is. Is it 7 or is it 1?

Kyriacos
Kyriacos el 23 de Abr. de 2012
hello all and thanks a lot for your help, all comments turned out to be very useful. @Richard Brown: Indeed there was a variable kk in the loaded file that posed problems. @Jan Simon: I followed your advise in retrieving the data in the way you suggested. indeed it is saver.
Best, Kyriacos
  1 comentario
Daniel Shub
Daniel Shub el 23 de Abr. de 2012
If the problem is solved please accept an answer and vote for the answers that were helpful to you.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by