Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to Plot Multiple variables ?(not multi-plot)

Asked by puru on 29 Jun 2011

I have generated 1000's of variables using Eval,I Want to plot a graph.Let's say below given are the different variables

t_1
t_2
t_3
.
.
t_n

I want to plot these against some other data. I used following to generate graph.

for i=1:1000
eval(sprintf('plot(t_%d)',i))
end

I could not able to plot the graph, it displays only a empty graph.Can anyone suggest some ideas please.

0 Comments

puru

Products

No products are associated with this question.

4 Answers

Answer by Paulo Silva on 29 Jun 2011

Bad idea to do it that way but I think you are missing

axes
hold on

before the for loop

0 Comments

Paulo Silva
Answer by Oleg Komarov on 29 Jun 2011

Very bad practice and as a proof you already have problems for such a simple task. Use dynamic fields in structures or cell arrays instead. Read carefully faq 4.6

0 Comments

Oleg Komarov
Answer by Matt Fig on 29 Jun 2011

As others have said, this kind of thing is the very reason why you should not make such variables using EVAL in the first place. You will have such problems every time you need to use these variables. Your code will be harder to debug and slower too, than if you had done the work with cell arrays or structures.

0 Comments

Matt Fig
Answer by Fangjun Jiang on 30 Jun 2011

The strangest way of using plot() I have even seen! Even plot(eval('t_1')) is better in my opinion.

With that said, assume your t_1, t_2, etc. are already there, you can convert it to cell array and then plot.

clear all;
t_1=1:5;
t_2=5:-1:0;
t_3=rand(6,1);
save;
Data=load;
CellData=struct2cell(Data);
figure;
hold on;
cellfun(@plot,CellData); 

0 Comments

Fangjun Jiang

Contact us