简体   繁体   中英

How do I make a plot in Matlab if I do not know the specific size of the array?

I have some code

function runTubulin()
  n = 10;
  for j = 1:n
      TubulinModel(); 
  end


plot(TubulinModel(), n);

So my problem is that TubulinModel has a random number of outputs So I keep getting

??? Error using ==> TubulinModel Too many output arguments.

Error in ==> runTubulin at 11 plot(TubulinModel(), n);

Is there A way to plot the data when I do not know the size of the array?

The error you are getting ( Too many output arguments ) implies that the function TubulinModel doesn't actually return any outputs. The function TubulinModel is expected to pass at least one output argument for the PLOT command to use, which it doesn't appear to be doing. You can check this by trying the following:

a = TubulinModel();  %# Place the output in a variable instead

If this gives you an error, then it means you will have to modify TubulinModel so that it returns the data you need, or places that data in a global variable that you can access outside of the function and use for the plot.

Your loop doesn't appear to do anything different with the TublinModel function on subsequent iterations. Also, the plot function calls the function again, the same way the loops did. Assuming different data of random lengths is returned by each loop, you can store each set of data in an object array, then find out what parameters to use before plotting.

function runTubulin()

n = 10;
max_length = 0; max_pos = 0; max_neg = 0;
for j = 1:n
    data{j} = TublinModel();    % get your data, then characterize it
    if max(data(j)) > max_pos, max_pos = max(data(j)); end
    if max(-data(j)) > max_neg, max_neg = max(-data(j)); end
end

figure(1); % new axes
axis([0 10 -max_neg max_pos]); hold on; % scale the axis and freeze it
for j = 1:n
    plot(length(data(j)),data(j));
end

Hope that helps!

When you call plot with two parameters, the first will be the x-axis data, and the second the y-axis data. Is this what you intend? If you want TubulinModel() to be the y-axis data, you can do plot(TubulinModel()) . See help plot for more information.

I don't understand why you call TubulinModel() ten times in the loop before calling it an eleventh time in plot ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM