How do I convert a symbolic expression to a string?
38 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
John D'Errico
2024-10-24,14:42
编辑:John D'Errico
2024-10-24,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);
更多回答(1 个)
Voss
2024-10-24,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);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!