Printing equations stored in a cell to plot titles?

조회 수: 1 (최근 30일)
Braden Kerr
Braden Kerr 2020년 11월 26일
댓글: Ameer Hamza 2020년 11월 26일
Hello,
Currently i have multiple equations stored a cell array and would like to print them as the titles to various plots. Since I am calling the cells in a loop I would like to do soemthing like this but I dont think sprintf is the correct way to apprach it.
q_array = {@(x)ones(size(x)); @(x) abs(x); @(x) x.^2; @(x) (1/3)+(2/3)*x.^2};
for k =1:4
%Code that generates the plot for each loop
formatspec = 'Exact vs Scheme Plots for q({\nu}) = %s';
sgtitle(sprintf(formatspec, q_array{k}));
end
Thank you

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 26일
편집: Ameer Hamza 2020년 11월 26일
Although you can use func2str() to convert the function handle to char array
sgtitle(sprintf(formatspec, func2str(q_array{k})));
but a more flexible solution is to create a seperate array for titles
q_array = {@(x)ones(size(x)); @(x) abs(x); @(x) x.^2; @(x) (1/3)+(2/3)*x.^2};
q_titles = {'ones(size(x))'; 'abs(x)'; 'x^2'; '(1/3)+(2/3)*x^2'};
for k =1:4
%Code that generates the plot for each loop
figure;
formatspec = 'Exact vs Scheme Plots for q({\\nu}) = %s';
sgtitle(sprintf(formatspec, q_titles{k}));
end
It gives more control over what appears in the title.
  댓글 수: 2
Braden Kerr
Braden Kerr 2020년 11월 26일
Thank you
Ameer Hamza
Ameer Hamza 2020년 11월 26일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by