Multiple plots in a for loop combined with variable cell arrays

조회 수: 1 (최근 30일)
Patrick Petre
Patrick Petre 2020년 11월 27일
댓글: Patrick Petre 2020년 11월 27일
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')

채택된 답변

Stephen23
Stephen23 2020년 11월 27일
편집: Stephen23 2020년 11월 27일
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
Patrick Petre
Patrick Petre 2020년 11월 27일
oh thank you.. i actually had hold on after all of the plots but seems like i need one after every plot. thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by