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

4 views (last 30 days)
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

Answers (2)

Nirmal Gunaseelan
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
Michael
Michael on 10 Jun 2011
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 on 10 Jun 2011
I should add that at all 3 breakpoints, the command "find(zosearch==z(158))" (the one that's causing trouble) works.

Sign in to comment.


Paulo Silva
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
Michael
Michael on 10 Jun 2011
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!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!