Plotting cell arrays, arbitrary size

4 views (last 30 days)
Benjamin
Benjamin on 26 Feb 2012
I'm trying to plot an arbitrary number of curves contained in a 2xN cell array. Row 1 is the XData, Row 2 contains the YData. I have verified all vectors (of doubles) are the same length.
The code:
[C{1,k},C{2,k}]=MyFun(...);
plot(C{:})
returns the error "Too many input arguments." I have tested with the size of C as 2x1 and 2x2.
I ensure that the output vectors generated in MyFun are row vectors with (though I think this is unnecessary):
z=reshape(z,[1 numel(z)]);
Oddly enough, I have taken this idea (plot(C{:})) from other code that works perfectly.
Any thoughts as to what is causing the error and how to fix?

Answers (2)

Jan
Jan on 26 Feb 2012
Please post the type and size of the two outputs of your MyFun function and explain, what "k" is. If "plot(C{:})" works depends on the contents of the cell C.
Perhaps this works - but this is pure guessing:
plot(C{:, k})
I have no idea, what z is and why you have to reshape it.
  4 Comments
Benjamin
Benjamin on 27 Feb 2012
Just a mistake in the write up. Not in the code. I'm still having the error problem.
Jan
Jan on 27 Feb 2012
Do the vecors in C all have the same length? If so, you can create a matrix instead:
for k = length(c):-1:1 % Backwards for pre-allocation
[C1(k, :), C2(k, :)] =MyFun(a(k), b(k));
end

Sign in to comment.


Benjamin
Benjamin on 28 Feb 2012
Turns out plotting a cell array using "plot(C{:})" works perfectly well...so long as you haven't overloaded the plot function.
I accidentally named one of my functions "plot" in the same .m file. It was declared "function []=plot()". Because it was in the same .m file, it took precedence over the built-in function AND since I had declared zero inputs, any number of inputs would give the error I was seeing!
I found my error late last night.
Thank you for all your help! Ben

Community Treasure Hunt

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

Start Hunting!