Multiple plots in a for loop combined with variable cell arrays

1 view (last 30 days)
Okay so i think my title sounds more difficult then it actually is.
So as a basic example: I have 2 Sets of Data, lets say y=x and y=2x, both range from 0 to 10. I want to plot both of them but i specificly want to look at x between 2 and 6. As i sometimes have more datasets all of this is in a for loop.
I Split each set of data into 3 pieces. 1. irrelevant range 0-2
2. relevant range 2-6
3. irrelevant range 6-10
All of this worked BUT now my problem: I try to plot them inside the loop like u see below with i beeing my loop index. But no matter the order there is always the second dataset of the first to plots missing. In the example below x_irrelevant2{2} and x_relevant{2} are missing and i dont know why.
plot(x_irrelevant2{i},y_irrelevant2{i},'color',[0.5 0.5 0.5],'Marker','.','linestyle','none')
plot(x_relevant{i},y_relevant{i},'color',c{i},'Marker','.','linestyle','none')
plot(x_irrelevant1{i},y_irrelevant1{i},'color',[0.5 0.5 0.5],'Marker','.','linestyle','none')

Accepted Answer

Stephen23
Stephen23 on 27 Nov 2020
Edited: Stephen23 on 27 Nov 2020
By default every plot call deletes the contents of the figure before plotting the new data. So if you want to call plot multiple times and not remove any existing lines or markers, then you need to tell MATLAB:
plot(...) % first plot
hold('on') % <- you need this
plot(...) % other plots
hold('off') % afterwards
Read more:
  1 Comment
Patrick Petre
Patrick Petre on 27 Nov 2020
oh thank you.. i actually had hold on after all of the plots but seems like i need one after every plot. thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!