command doesn't work from inside a loop in a script but works fine in the command window

조회 수: 3 (최근 30일)
Hi,
As part of a bigger while loop I am using a for loop to create an array that contains the "intersection" of two other arrays (z and zosearch). i.e. contains only the elements that are common to both. I then want to take all the entries in another array (zy) that correspond to these entries. To do this, I use the following code:
for j = 1:1001
zoindices=find(zosearch==z(j));
if size(zoindices,1)>0
zyofzfound(j)=zy(min(zoindices));
end
end
zofoundindices = find(hozfound);
foundzo0=z(zofoundindices);
which produces the vectors foundzo0 and zyofzfound that I want.
The whole thing works fine (though slowly) on my computer running Matlab 7.10.0 (R2010a). However, when I try and run it on a faster computer but one that runs Matlab 7.8.0 (R2009a), I get the following error:
Undefined function or method 'z' for input arguments of
type 'double'.
Error in ==> optimaltax at 83
zoindices=find(zosearch==z(j));
Somehow matlab seems to be "forgetting" that z is an array not a function. The really weird thing is that if I just type
>> zoindices=find(zosearch==z(j))
zoindices =
Empty matrix: 0-by-1
>> In the command window straight after it works beautifully. I've tried taking this for loop out of the larger while loop and running it by itself and it works fine, it only refuses to work when it's inside the bigger loop (but the whole thing runs fine on my computer...)
Any help/thoughts much appreciated
Michael

답변 (2개)

Nirmal Gunaseelan
Nirmal Gunaseelan 2011년 6월 10일
There are a couple of possible reasons:
1. Like Paulo said, there could be a file called z.m - it will be picked up by MATLAB even if it is not in the same folder, but on the path. You could confirm this by typing, at the command prompt:
which -all z
2. How is the vector z created? I suspect that z does not exist in the right workspace (or not at all) when you are inside the loop. One way to check is to place a breakpoint on the for loop line and type, at the command prompt:
who
This should list the variables in the workspace and maybe z isn't there now?. The Workspace pane in the MATLAB desktop should have a dropdown list for various workspaces (if you are using functions) - make sure you are in the right one.
  댓글 수: 3
Michael
Michael 2011년 6월 10일
Hi Nirmal,
Thanks for this that's very helpful.
1: this tells me "z is a variable" which is fine.
2: I put in breakpoints just before the loop, just when it opens and at the first line of commands inside the loop and in all three z appears in the list of variables.
Thanks again,
Michael
Michael
Michael 2011년 6월 10일
I should add that at all 3 breakpoints, the command "find(zosearch==z(158))" (the one that's causing trouble) works.

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


Paulo Silva
Paulo Silva 2011년 6월 10일
Do you have one m file or function called z.m? having one can cause the error:
Undefined function or method 'z' for input arguments of type 'double'.
  댓글 수: 2
Michael
Michael 2011년 6월 10일
It's all in one m file. z is 1001x1 vector, that's what's confusing me. There's no file called z.m in the same folder so it should understand. Plus, the exact same code works fine on my computer...
Thanks!

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

카테고리

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