is it possible to call variable from text in MATLAB Live Script ?

31 visualizaciones (últimos 30 días)
Thanacha Choopojcharoen
Thanacha Choopojcharoen el 5 de Dic. de 2019
Respondida: Emily el 18 de En. de 2024
is it possible to call variable from text in MATLAB Live Script , so that the text can be changed based on that variable?
For example :
n = 2;
In order to find the ouput, you must divide the input with the value "2".
If I change n to 3, I want my text to change as well.
n = 3;
In order to find the ouput, you must divide the input with the value "3".
Is it possible ?
  1 comentario
Hugh Wheaton
Hugh Wheaton el 9 de Abr. de 2020
Editada: Hugh Wheaton el 9 de Abr. de 2020
I'd like to add that I could use this too. It seems rather unnecessary, but I get complaints from my tutors when they say it's hard to read the output of the code (formatting in general is a bit difficult if you are to submit a pdf too but that's a bit more complex an issue, and more specific to undergrad work). This seems very specific an issue, but I'd imagine the utility of the livescript feature would become much more versatile should you be able to flexibly print a document with embedded functions with underlying calculations.
One example I could think of is designing a multitude of simulations for different configurations, designing a template that formats all the details, methodology and results as seen fit to be easily processed, and then running a script that prints like 100 of them to PDF to be reviewed.
I can't imagine it would be too hard to design some type of functionality, say similar to how tex is implemented, where you can simply define a string and then reference it to be printed with matching format. They do it for titles, axes, labels and annotations, so why can't it be done for text?

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 5 de Dic. de 2019
Editada: Walter Roberson el 5 de Dic. de 2019
n = 3;
fprintf('In order to find the output, you must divide the input with the value "%d".\n', n);
See also compose()
  3 comentarios
Walter Roberson
Walter Roberson el 9 de Abr. de 2020
As far as I know, it is not possible to generate the markup on-the-fly, not for text and not for equations.
It is also not possible to output as interpreted latex inline or as command output, except by text() onto a graph or some of the other ways that text can be put into a figure.
Arun Tangirala
Arun Tangirala el 19 de Abr. de 2020
That's a pity. It would be certainly useful to insert values of variables in the text in the way it is done in several other markdown documents (e.g., R Markdown). I truly hope MathWorks will consider incorporating this feature in their next release. And personally I think it should not involve a great deal of effort in doing so since it is all about including a new control or interpreter that refers to the MATLAB Live Editor's workspace for that variable and inserts it.

Iniciar sesión para comentar.


Emily
Emily el 18 de En. de 2024
I agree with Walter's fprintf answer, however I found a tricky way to get the livescript to also display latex equations that include values of the variables: Make a plot, give it a title, then hide the axes and resize the plot.
I want to let a user define values for some variables, and then display the value of each variable in latex form. Here is my code for doing that:
% I'd like to display the value of these variables using latex, but can only output latex as a title of a figure. So make a figure, then hide all but the title.
Output_vals_string_2 = sprintf('For two-step model: $ \\tau_t = %g s; \\tau_{CM} = %g s; \\tau_{CR} = %g s $ ',Tunnel_rate_s, Collapse_rate_Manip_s, Collapse_rate_Readout_s);
Output_vals_string_std = sprintf('For standard model with dephasing noise: $ \\tau_t = %g s; \\tau_{DM} = %g s; \\tau_{DR} = %g s$ ',Tunnel_rate_s, 10^(-6)*Collapse_rate_Manip, 10^(-6)*Collapse_rate_Readout);
fh1 = figure; fh1.Units='normalized';
subplot(1,2,1);plot(Tunnel_rate_s); title(Output_vals_string_std,'interpreter','latex'); set(gca, 'visible', 'off'); set(findall(gca, 'type', 'text'), 'visible', 'on');
subplot(1,2,2); plot(Tunnel_rate_s); title(Output_vals_string_2,'interpreter','latex'); set(gca, 'visible', 'off'); set(findall(gca, 'type', 'text'), 'visible', 'on');
fh1.Position(3:4) = [1,0.1]; fontsize(14, "points");
It happened that I had two sets of variables I wanted to output, so I put them in the title of two different subplots, side by side. If you only had one you could leave out the subplot commands and just make one plot.
This is definitely suboptimal and I would wish to be able to directly output a latex string which displays the values the user just chose, but better than nothing.

Community Treasure Hunt

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

Start Hunting!

Translated by