Strange problem - vector dimensions
显示 更早的评论
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]
采纳的回答
更多回答(3 个)
Jan
2012-4-21
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 个评论
Richard Brown
2012-4-21
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
2012-4-21
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
2012-4-23
0 个投票
1 个评论
Daniel Shub
2012-4-23
If the problem is solved please accept an answer and vote for the answers that were helpful to you.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!