How to add an index to graph title, legend, and output image without having to write everywhere the same number

조회 수: 25 (최근 30일)
MOTIVATION: I want that a predefined number be used in the following:
1) generation of a graph,
2) title of the graph,
3) legend of the graph,
4) name of the output file.
EXAMPLE: If I define the index equal to 2 as written in the script below,
INDEX=2;
x = 0:pi/100:2*pi;
y = sin(x-INDEX);
h=plot(x,y);
legend(h,'sin(x-INDEX)',1)
title('TEST INDEX','FontSize',26, 'FontName','Arial', 'FontWeight','bold')
print -dtiff -f1 -r600 OUTPUT_INDEX %or -dbitmap, -djpeg, -dtiff, -depsc
I WISH: that Matlab uses that value to generate the graph (as it already does), but also writes the name everywhere with that index. Thus the expectation is the following:
2) title of the graph: TEST 2 and not TEST INDEX
3) legend of the graph: sin(x-2) and not sin(x-INDEX)
4) name of the output file: OUTPUT_2 and not OUTPUT_INDEX
I wonder if someone could help me how to write these commands correctly.
Thanks for your attention
Emerson

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 12월 15일
what you need is num2str(INDEX) or sprintf()
f=figure(INDEX);
legend(h,sprintf('sin(x-%d)',INDEX),1);
title(['TEST ',num2str(INDEX));
The file name can be specified as ['OUTPUT_',num2str(INDEX)]
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 12월 16일
To do that, you need to use the function format of print(), like
print(f,'-dtiff',['OUTPUT_',num2str(INDEX)])
Emerson De Souza
Emerson De Souza 2011년 12월 16일
Thank you again Fangjun,
now it works well.
These command lines will
save a lot of my time.
Wish you a nice evening
Emerson

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by