Clear Filters
Clear Filters

Passing array to a function

66 views (last 30 days)
Tom
Tom on 8 Mar 2011
Hi,
I'm getting an error message when attempting to pass some variables (including arrays), and can't figure out why.
Here is my code..
frequency(x) = getFreq(ts, Rdata, Ldata, foot);
Ldata is a 819x3 double, Rdata is a 1362x3 double, foot=1, ts=10959
On the function side, the parameters are
function Freq = getFreq(timestamp, SessionL, SessionR, foot)
The error message I'm getting is
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> main at 71 frequency(x) = getFreq(ts, Rdata, Ldata, foot);
Thanks

Answers (3)

Teja Muppirala
Teja Muppirala on 8 Mar 2011
Whatever Freq is when you exit the function getFreq, it is not of a size compatible with the index vector x.
For example, this will also give you the same error.
A = [0 0 0];
x = [1 3];
A(x) = sum( rand(3) )
This fails because x only has two values, but the sum returns a vector of 3 values. This, however, is OK:
A = [0 0 0];
x = [1 3];
A(x) = sum( [1 2; 3 4] )
So just go make sure that the value getting returned (Freq) is of a proper size to match with x.
Try the following:
temp = getFreq(ts, Rdata, Ldata, foot);
size(temp)
size(x)
and compare what you get. That should help you see why it's not working.

Brett Shoelson
Brett Shoelson on 8 Mar 2011
You'll need to show us a bit more. It would be particularly useful, for instance, to see line 71 of getFreq.
Consider putting a breakpoint there and re-running. Are the variables what you expect when you get to that point? If so, you have a logical flaw in your code. If not, trace backward and figure out how/why something got changed.
Brett
  1 Comment
Tom
Tom on 8 Mar 2011
sorry i should have stated, line 71 is the first line shown, the line calling the function. The variables are how i expected, I just cant understand why that type of error would occur

Sign in to comment.


Matt Tearle
Matt Tearle on 8 Mar 2011
What Teja said.
I hate to ask the dumb question, but do you actually want to do indexing on line 71? Many people write things like foo(x) = ... because they're used to thinking in mathematical terms (foo is a function of x). But [voice = Morbo] "MATLAB does not work that way" [/voice]. In MATLAB, foo(x) = ... means "calculate the RHS (...) and put the result into the elements of foo indexed by x". So the fundamental dumb question to ask is: are you really trying to save the output from getFreq into a subset of the variable frequency, in which case do what Teja said to check dimensions, or are you actually just trying to save the output of getFreq to a variable called frequency, in which case get rid of the (x).
  1 Comment
Matt Tearle
Matt Tearle on 8 Mar 2011
Er, just to clarify: "dumb" question not because *you're* being dumb, but because *I* feel dumb asking, when it's entirely possible that, yes, you do want to do that.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!