How do I convert a symbolic expression to a string?
38 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lukas
el 24 de Oct. de 2024 a las 14:36
Comentada: Lukas
el 24 de Oct. de 2024 a las 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 comentarios
Respuesta aceptada
John D'Errico
el 24 de Oct. de 2024 a las 14:42
Editada: John D'Errico
el 24 de Oct. de 2024 a las 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);
Más respuestas (1)
Voss
el 24 de Oct. de 2024 a las 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);
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!