필터 지우기
필터 지우기

expand array within for loop

조회 수: 2 (최근 30일)
Josefina Ottitsch
Josefina Ottitsch 2019년 4월 3일
댓글: Walter Roberson 2019년 4월 3일
hello,
I have this code:
close all
x=[0,1,2,3,4,5]
x1=sin(x)
p1=x1(2)
p2=x1(3)
x3=cos(x)
p4=x3(2)
p5=x3(3)
figure(1)
plot(x1)
hold all
plot([2],[p1],'o')
plot([3],[p2],'o')
plot(x3)
plot([2],[p4],'o')
plot([3],[p5],'o')
k=2
for k=1:k
ls1=[num2str(k),'.Q']
ls2=[num2str(k),'.SR']
ls3=[num2str(k),'.r']
end
legend(ls1,ls2,ls3)
and I would like to fix the legend. I want my program to tell me that the 6 legend variables are: 1.Q,1.SR,1.r,2.Q,2.SR,2.r
Right now the program is overwriting the outcome of the for loop.
How do I do this?
Thank sou yo much!

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 3일
legend( [1 2] + [".Q"; ".SR"; ".r"] )
Requires R2017a or later.
  댓글 수: 2
Josefina Ottitsch
Josefina Ottitsch 2019년 4월 3일
thank you so much!
Walter Roberson
Walter Roberson 2019년 4월 3일
Note that for this purpose it was important that the first part [1 2] be a row vector and the second part [".Q"; ".SR"; ".r"] be a column vector, and that R2016b or later were used, as "implicit expansion" is used here to get all combinations of the two. The restriction to R2017a or later is because the string object syntax "" was introduced in R2017a. There is a way to extend the use of string objects to R2016b as well. String objects did not exist before R2016b, so for earlier releases a different approach would have to be used.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by