Graph With Variable Number of Plots

I'm writing a piece of code that needs to split up a vector into smaller vectors which are not uniform in length. These smaller vectors then need to be plotted (against another set of smaller vectors which have undergone the same process). I've wrote a loop which gives each smaller vector a similar name using the eval and num2str expressions.
This works to the point that it'll allow me to splot my big vector up and gives me an unlimited number of smaller vectors, each with their own name. However I have a problem when I come to plot them; I don't know how many smaller vectors I will have until I run the code as the places they get cut will depend on initial conditions.
If I use the following code as an very crude example;
for B=1:8
eval(['M' num2str(B) ' = [2 3].^B' ])
eval(['N' num2str(B) ' = [2 4].*B' ])
end
plot(M1,N1,'k',M2,N2,'k',M3,N3,'k',M4,N4,'k',M5,N5,'k',M6,N6,'k', ...
M7,N7,'k',M8,N8,'k')
The above simulates how my code sets the cut up vectors as dynamic names which I can then plot later. Although this code doesn't do it those vectors are of unknown length and number (before I run the code). For example in my actual code M4 and N4 may be twice as long as M5 and N5, and it may only go up to M3 and N3.
Is there any way anyone can think of that my plot code can automatically change how many things are being plotted depending on initial conditions? So for example, in the above case if I changed the value of B to 3 the code would automatically only plot M1 against N1, M2 against N2 and M3 against M3.
The only thing I can think of is a for loop and multiple plot commands but that's far for ideal.
Any help would be appretiated and if there's any questions I'm happy to provide more information.
UPDATE:
An new example where the size of the vectors is not constant
for B=1:8
eval(['M' num2str(B) ' = rand(1,B)' ])
eval(['N' num2str(B) ' = rand(1,B)' ])
end
plot(M1,N1,'b',M2,N2,'b',M3,N3,'b',M4,N4,'b',M5,N5,'b',M6,N6,'b', ...
M7,N7,'b',M8,N8,'b')

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 14 Nov 2012
Edited: Azzi Abdelmalek on 14 Nov 2012
hold on
for B=1:8
M = [2 3].^B
N = [2 4].*B
plot(N,M,'k')
end
If you want to store the result it's better to use for eample
for B=1:8
M.(sprintf('v%d',B))= [2 3].^B
N.(sprintf('v%d',B))= [2 4].*B
end

10 Comments

The first part of your suggestion unfortunatly won't work as the vectors are not uniform length so M might be 1 by 8 for B = 1 but it might be 1 by 3 for B = 2, thus storing them in a matrix isn't really an option as far as I can see. Sorry for my example code not reflecting this (at the time I wasn't sure how to simulate that issue witout using my actual code) I'll try and improve it to better reflect the added complexity.
As for the second storage method, that seems good, but out of interest how would I go about calling the values back up if I say wanted the value of M at B = 3?
If your vector are not uniform, then use cellarray
to get M at B=3,
M.(sprintf('v%d',3))
hold on
A={[1 2 3],[5 4],[1 4 7 8]};
for B=1:numel(A)
M.(sprintf('v%d',B))= A{B}.^B
N.(sprintf('v%d',B))= A{B}.*B
end
or
hold on
A={[1 2 3],[5 4],[1 4 7 8]};
n=numel(A)
M=cell(n,1);
N=M;
for B=1:n
M{B}= A{B}.^B
N{B}= A{B}.*B
plot(M{B},N{B},'k')
end
Ok, with a bit of fiddling I'm sure I could store them like that in my actual code. Is there a special way I call them back and/or plot them?
P.S. I've edited my original post to include an example where the length of the vectors is not constant (it's length goes up with B which my code doesn't (it's 'random') but it should give a better idea of what I'm working with)
Look at my above comment
Ok, brilliant. I think I get it and that I can apply that to my actual code. Thank you very much for the help, very appretiated and if all works out I'll make sure to accept this answer :)
Ok, it's working which is brilliant :) My final question is, it plots fine when I put the plot command in the for loop with the calculations or when I do them individually at the end (i.e. plot(M{1},N{1},etc.)) but is there a way to get it to do them all at the end without a loop running through different values? If not it's no worry, like I say it does work; just like keeping all my plots at the end of my code on their own if I can help it.
Thanks for the help thus far by the way
use this
hold on;arrayfun(@(x) plot(M{x},N{x},'k'),1:numel(M))
Works perfectly; thank you very much for all the help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!