Strange problem - vector dimensions
2 views (last 30 days)
Show older comments
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]
0 Comments
Accepted Answer
Richard Brown
on 21 Apr 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 Comments
More Answers (3)
Jan
on 21 Apr 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 Comment
Richard Brown
on 21 Apr 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' :-)
Image Analyst
on 21 Apr 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?
0 Comments
Kyriacos
on 23 Apr 2012
1 Comment
Daniel Shub
on 23 Apr 2012
If the problem is solved please accept an answer and vote for the answers that were helpful to you.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!