command doesn't work from inside a loop in a script but works fine in the command window
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Nirmal Gunaseelan
on 10 Jun 2011
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 Comments
Paulo Silva
on 10 Jun 2011
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!