필터 지우기
필터 지우기

Strange problem - vector dimensions

조회 수: 1 (최근 30일)
Kyriacos
Kyriacos 2012년 4월 21일
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]

채택된 답변

Richard Brown
Richard Brown 2012년 4월 21일
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
Richard Brown
Richard Brown 2012년 4월 21일
Ok, is there a variable called 'kk' in it?
Daniel Shub
Daniel Shub 2012년 4월 23일
+1 for your comment about kk.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jan
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
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
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
Kyriacos 2012년 4월 23일
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
Daniel Shub
Daniel Shub 2012년 4월 23일
If the problem is solved please accept an answer and vote for the answers that were helpful to you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by