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 件のコメント

Govind KM
Govind KM 2024 年 12 月 4 日
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 2024 年 12 月 4 日
編集済み: feynman feynman 2024 年 12 月 4 日
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 2024 年 12 月 4 日
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 2024 年 12 月 4 日
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.

サインインしてコメントする。

 採用された回答

Les Beckham
Les Beckham 2024 年 12 月 4 日

1 投票

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 件のコメント

Walter Roberson
Walter Roberson 2024 年 12 月 4 日
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 2024 年 12 月 4 日
編集済み: Les Beckham 2024 年 12 月 4 日
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 2024 年 12 月 5 日
thanks a lot, then for that reason above how to modify the code provided by @Les Beckham?
Les Beckham
Les Beckham 2024 年 12 月 5 日
編集済み: Walter Roberson 2024 年 12 月 5 日
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
feynman feynman
feynman feynman 2024 年 12 月 29 日
@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 2024 年 12 月 29 日
It depends on whether you are executing the mlx from the command line or from the editor.
feynman feynman
feynman feynman 2024 年 12 月 29 日
編集済み: feynman feynman 2024 年 12 月 29 日
I executed the above mlx in .mlx editor and yielded the following
Walter Roberson
Walter Roberson 2024 年 12 月 29 日
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 2024 年 12 月 30 日
it works thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation of 2-D Selections in 3-D Grids についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by