How do I convert a symbolic expression to a string?

조회 수: 19 (최근 30일)
Lukas
Lukas 2024년 10월 24일
댓글: Lukas 2024년 10월 24일
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일
편집: John D'Errico 2024년 10월 24일
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일
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일
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