How do I convert a symbolic expression to a string?
17 views (last 30 days)
Show older comments
Lukas
on 24 Oct 2024 at 14:36
Commented: Lukas
on 24 Oct 2024 at 14:49
Hello all,
I want to write a script that uses two symbolic expressions chosen by the user and those should be written as a string in the title of the final plot. The relevant snippets are:
syms x
% the arbitrary expressions
phi_1 = x;
phi_2 = x^2;
% example plot
example = [1:10];
plot(example)
% Interpreter: LaTeX
interpr = 'LaTeX';
set(groot, 'DefaultTextInterpreter', interpr);
set(groot, 'DefaultAxesTickLabelInterpreter', interpr);
set(groot, 'DefaultAxesFontName', interpr);
set(groot, 'DefaultLegendInterpreter', interpr);
% my try at including the expressions in the title
title('lorem ipsum $ \phi_\mathrm{1} = ', string(phi_1), ' $ dolor sit $ \phi_\mathrm{2} = ', string(phi_2), ' $ ', 'FontSize', 14);
When I try to plot this, I get the errors you will see when running this snippet. Though I found similar questions, none of the solutions applied to my problem, so I would be very grateful for any help :)
Thank you!
0 Comments
Accepted Answer
John D'Errico
on 24 Oct 2024 at 14:42
Edited: John D'Errico
on 24 Oct 2024 at 14:44
Simple enough. Note the changes I made to your last line only. I could also have done it using character vectors, but then I would need to concatenate them using []. You cannot just put commas between the pieces and have title be able to guess what you intended.
syms x
% the arbitrary expressions
phi_1 = x;
phi_2 = x^2;
% example plot
example = [1:10];
plot(example)
% Interpreter: LaTeX
interpr = 'LaTeX';
set(groot, 'DefaultTextInterpreter', interpr);
set(groot, 'DefaultAxesTickLabelInterpreter', interpr);
set(groot, 'DefaultAxesFontName', interpr);
set(groot, 'DefaultLegendInterpreter', interpr);
% my try at including the expressions in the title
title("lorem ipsum $ \phi_\mathrm{1} = " + string(phi_1) + " $ dolor sit $ \phi_\mathrm{2} = " + string(phi_2) + ' $ ','FontSize', 14);
More Answers (1)
Voss
on 24 Oct 2024 at 14:42
Here are a couple of options:
syms x
% the arbitrary expressions
phi_1 = x;
phi_2 = x^2;
% example plot
example = [1:10];
plot(example)
% Interpreter: LaTeX
interpr = 'LaTeX';
set(groot, 'DefaultTextInterpreter', interpr);
set(groot, 'DefaultAxesTickLabelInterpreter', interpr);
set(groot, 'DefaultAxesFontName', interpr);
set(groot, 'DefaultLegendInterpreter', interpr);
title(['lorem ipsum $ \phi_\mathrm{1} = ', char(phi_1), ' $ dolor sit $ \phi_\mathrm{2} = ', char(phi_2), ' $ '], 'FontSize', 14);
syms x
% the arbitrary expressions
phi_1 = x;
phi_2 = x^2;
% example plot
example = [1:10];
plot(example)
% Interpreter: LaTeX
interpr = 'LaTeX';
set(groot, 'DefaultTextInterpreter', interpr);
set(groot, 'DefaultAxesTickLabelInterpreter', interpr);
set(groot, 'DefaultAxesFontName', interpr);
set(groot, 'DefaultLegendInterpreter', interpr);
title("lorem ipsum $ \phi_\mathrm{1} = " + string(phi_1) + " $ dolor sit $ \phi_\mathrm{2} = " + string(phi_2) + " $ ", 'FontSize', 14);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!