Printing equations stored in a cell to plot titles?

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Ameer Hamza
Ameer Hamza il 26 Nov 2020
Modificato: Ameer Hamza il 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.

Più risposte (0)

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by