Printing equations stored in a cell to plot titles?

1 vue (au cours des 30 derniers jours)
Braden Kerr
Braden Kerr le 26 Nov 2020
Commenté : Ameer Hamza le 26 Nov 2020
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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 26 Nov 2020
Modifié(e) : Ameer Hamza le 26 Nov 2020
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 commentaires
Braden Kerr
Braden Kerr le 26 Nov 2020
Thank you
Ameer Hamza
Ameer Hamza le 26 Nov 2020
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by