Hello,
'parnam' contains 8 1x1 structures (data and text). The data contains the latitude, longitude and altitude of a sensor. I want to create a legend for my plot that states the latitude of each plot, but it only prints the first.
figure
for k=1:m
scatter(x(:,k),y(:,k)), hold on
legend(sprintf('aircraft at %2.2f degrees latitude', parnam{1,k}.data(1,1)))
end
hold off

 채택된 답변

dpb
dpb 2017년 5월 16일

0 개 추천

There's only one legend per plot; you're overwriting each pass thru the loop.
for k=1:m
scatter(x(:,k),y(:,k))
if k==1, hold on, end % can't get an "onner"
end
legend(num2str(parnam{1,1:m}.data(1,1)).','aircraft at %2.2f degrees latitude')

댓글 수: 3

Alex
Alex 2017년 5월 16일
편집: Alex 2017년 5월 16일
??? Bad cell reference operation.
MATLAB doesnt like "...{1,1:m}".
This works though so thank you:
figure
for k=1:m
scatter(x(:,k),y(:,k)), hold on
h(k,:) =sprintf('aircraft at %2.2f degrees latitude', parnam{1,k}.data(1,1)) ;
end
legend(h)
hold off
legend( cellstr( num2str( cellfun( @(C) C.data(1,1), parnam(1,1:m) ) .', 'aircraft at %2.2f degrees latitude' ) ) )
Alex, your version will not work if some of the data values have a different number of digits, such as 3.7 degrees or 13.7 degrees. But you could use
figure
for k=1:m
scatter(x(:,k),y(:,k)), hold on
h{k} = sprintf('aircraft at %2.2f degrees latitude', parnam{1,k}.data(1,1)) ;
end
legend(h)
hold off

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

추가 답변 (0개)

카테고리

태그

질문:

2017년 5월 16일

댓글:

2017년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by