Printing equations stored in a cell to plot titles?

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Nov 2020
Edited: Ameer Hamza on 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.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!