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!

采纳的回答

John D'Errico
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 个评论
Lukas
Lukas 2024-10-24,14:49
Thank you very much! I saw the commas in a similar post somewhere as their solution so I just tried it :)

请先登录,再进行评论。

更多回答(1 个)

Voss
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);

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by