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
Could you give an example for what you want to achieve? I'm a little confused, as a .mlx file will output figures inline and not in separate windows as you have mentioned.
feynman feynman
feynman feynman el 4 de Dic. de 2024
Editada: feynman feynman el 4 de Dic. de 2024
The myfunction.m plots in a separate window.
function myfunction
plot(1,1)
end
The mymlx.mlx with only the following single line plots in a separate window too, given 'myfunction.m' in the same directory.
myfunction
But mymlx.mlx with the following plots inline.
myfunction
function myfunction
plot(1,1)
end
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 4 de Dic. de 2024
It turns out to be a bit more complicated.
  • .m being run from command line: dbstack filename ends in .m
  • .m being run from editor: dbstack filename ends in .m
  • .mlx being run from command line: dbstack filename ends in .mlx
  • .mlx being run from editor: dbstack filename ends in .m and filename lives within tempdir
For example, .mlx being run from editor:
ST = struct with fields:
file: '/private/var/folders/b0/t3wd0gm1775gljwgsq6kwv780000gn/T/Editor_mjcux/LiveEditorEvaluationHelperEeditor658B6737_D855E04C.m'
name: 'LiveEditorEvaluationHelperEeditor658B6737_D855E04C'
line: 1
Les Beckham
Les Beckham el 4 de Dic. de 2024
Editada: Les Beckham el 4 de Dic. de 2024
Ah, yes. I had forgotten about that hidden layer of the "EvaluationHelper". Thanks for adding that additional information, Walter.
I suppose one could test for contains '.mlx' or contains 'LiveEditorEvaluationHelper'
feynman feynman
feynman feynman el 5 de Dic. de 2024
thanks a lot, then for that reason above how to modify the code provided by @Les Beckham?
Les Beckham
Les Beckham el 5 de Dic. de 2024
Editada: Walter Roberson el 5 de Dic. de 2024
I would try this
function myfunction()
d = dbstack;
fn = d(end-1).file; % get filename of caller
if contains(fn, '.mlx') || contains(fn, 'LiveEditorEvaluationHelper')
% do something in '.mlx mode'
else
% do something in '.m mode'
end
end
@Walter Roberson many thanks but in my .mlx, it identifies .mlx as .m but .mlx as in the following:
tellFileType
function tellFileType
clear;clc;
d=dbstack
filename=d.file
% filename=d(end-1).file
contains(filename, '.m')
contains(filename, '.mlx')
contains(filename, 'LiveEditorEvaluationHelper')
end
Walter Roberson
Walter Roberson el 29 de Dic. de 2024
It depends on whether you are executing the mlx from the command line or from the editor.
feynman feynman
feynman feynman el 29 de Dic. de 2024
Editada: feynman feynman el 29 de Dic. de 2024
I executed the above mlx in .mlx editor and yielded the following
Walter Roberson
Walter Roberson el 29 de Dic. de 2024
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
it works thank you very much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Dic. de 2024

Comentada:

el 30 de Dic. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by