Plot titles in for loop with string and variable names

조회 수: 11 (최근 30일)
Andrea Tersigni
Andrea Tersigni 2017년 2월 24일
댓글: Guillaume 2017년 2월 24일
I have a for loop that gives me several plots from sec. How can i make the several plots titles display both the value of sec and a string?
if true
plot(d(:,1),d(:,2),'LineWidth',1);
leg=legend('Stock price','Location','Best');set(leg,'FontSize',8);legend('boxoff');
datetick;title(sec{1});ylabel('USD'); % code
end

답변 (1개)

Guillaume
Guillaume 2017년 2월 24일
title(sprintf('Plot of stock %s', sec{1}));
The format specifier (%s in this case) depends on the type of variable (string, integer, real number) that you want to add to the string. Read the documentation of sprintf.
  댓글 수: 2
Andrea Tersigni
Andrea Tersigni 2017년 2월 24일
편집: Andrea Tersigni 2017년 2월 24일
In the for loop I also have the mean return and standard deviation for each security, can I use sprintf to display "The return for security ... is ... and the standard deviation is ..."? Both are not integers.
Guillaume
Guillaume 2017년 2월 24일
As I said, look at the documentation which includes all sort of examples.
sprintf('The return for security %s is %2.4g and the standard deviation is %2.4g', sec{1}, secmean, secstd)
Coming up with a format string that does what you want is not hard.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by