code to tell mlx or m files

41 visualizaciones (últimos 30 días)
feynman feynman
feynman feynman el 4 de Dic. de 2024
Comentada: feynman feynman el 30 de Dic. de 2024 a las 9:08
M and mlx files work slightly differently in outputting figures in separate windows or within the code file. I hope to have an m file that outputs figures in separate windows and another mlx file that outputs inline figures within the code file, so for various needs sometimes I use the m file and sometimes the mlx file. If there's an mlx code 'mymlx.mlx' that just runs 'myfunction' with a single line:
myfunction
There's a 'myfunction.m' in the same directory. The 'myfunction.m' is just regular code like:
function myfunction
...
end
I don't want to paste the same code of myfunction within 'mymlx.mlx'. Then in 'myfunction', is there a way to tell if the current code is in .m or .mlx? If it's in .m, I hope it runs a line that plots figures in separate windows but if it's in .mlx, I hope it runs a line that outputs inline figures within the code file. Without this extra control, both files output in separate windows.
  4 comentarios
Govind KM
Govind KM el 4 de Dic. de 2024
mymlx.mlx with the single line
myfunction
plots inline for me in R2024b. Could you try going to the View tab when the live script is open, and in the View section, selecting Output Inline?
feynman feynman
feynman feynman el 4 de Dic. de 2024
yes, sorry, for this sample code it does make no difference between .m and .mlx. But if there's push buttons in a figure, this makes a difference. When I run .mlx, I don't want push buttons to take effect so I need to deactivate them in the code. But in .m, I need them. This is why I need the code to tell by itself if it's in .m or .mlx.

Iniciar sesión para comentar.

Respuesta aceptada

Les Beckham
Les Beckham el 4 de Dic. de 2024
If you want your function to know if it was called from a .m file or a .mlx file you can do that using dbstack. However, I'm not aware of any way that the function could control where any plots that it made would appear. Here's an example of how your function could determine who called it.
function myfunction()
d = dbstack;
if contains(d(end-1).file, '.mlx')
% do something in '.mlx mode'
else
% do something in '.m mode'
end
end
  9 comentarios
Walter Roberson
Walter Roberson el 29 de Dic. de 2024 a las 20:31
Yes? That agrees with what I posted before,
  • .mlx being run from editor: dbstack filename ends in .m and filename lives within tempdir
feynman feynman
feynman feynman el 30 de Dic. de 2024 a las 9:08
it works thank you very much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by