How to get .m file definition programmatically?
古いコメントを表示
When looking at the Current Folder pane, the icons let you know if a m-file is a "script", "function", or "class" definition and if a .mlx is a "live script" or "live function". Is there any way to get that information programmatically using a built-in function?

7 件のコメント
Dyuman Joshi
2023 年 2 月 20 日
You can read the file name, specifically the extension in the form of a char or a string array and compare it accordingly.
dpb
2023 年 2 月 20 日
But an m-file can be either a script or a function from starters, so just having the file name is insufficient.
One has to parse the content of the file to know which. The internal interpreter does that so it knows; at the moment I'm unaware of any such user tool that returns that info (although I'm a couple rev's behind and haven't read the release notes for new features).
Dyuman Joshi
2023 年 2 月 20 日
編集済み: Dyuman Joshi
2023 年 2 月 20 日
Travis Bueter
2023 年 2 月 20 日
dpb
2023 年 2 月 20 日
Understanding the wish isn't the problem; it's that unless Yair has uncovered and published something recently, there's nothing exposed to the end user that tells you this.
The command window and interpreter have access to all the internals so they can do things such as this not available otherwise.
Now, that there may be something buried deep in the hidden bowels of the private sections...
Steven Lord
2023 年 2 月 20 日
my goal is to check if an arbritary .m file contains a class defintion, a function definition, or is a script.
How are you planning or hoping to use this information? What do you want to do differently depending on whether the file is a class file, a function file, a script file (and do you care if the script does or does not contain a local function?), or is not a MATLAB code file at all?
Travis Bueter
2023 年 2 月 20 日
編集済み: Travis Bueter
2023 年 2 月 20 日
回答 (2 件)
Sulaymon Eshkabilov
2023 年 2 月 20 日
It is possible differentiate .m and .mlx files by referring to their file extensions or simlarly, Simulink's .mdl and .slx or slxc files by using a wildcard, e.g.
Sm = dir('*.m') % Locates all M-files in the current directory
Sm.name % Names of M-files
numel(Sm) % How many M-file
% Similarly, MLX-files
Smlx = dir('*.mlx')
Smlx.name
numel(Smlx)
% Similarly, SLX files
Sslx = dir('*.slx')
Sslx.name
numel(Sslx)
Image Analyst
2023 年 2 月 20 日
Try using readlines then removing comments and see if the first line is classdef, function, or something else:
% Define filename.
fileName = 'testRGBImage.m';
% Read in all lines of a file.
allLines = readlines(fileName);
% Get rid of leading white space.
allLines = strtrim(allLines);
% Get rid of all lines that start with a comment.
c = startsWith(allLines, '%');
allLines(c) = [] % Delete lines starting with a comment.
% See if the first line of code is a classdef
itsAClass = contains(allLines{1}, 'classdef')
% See if the first line of code is a function declaration
itsAFunction = contains(allLines{1}, 'function')
% If it's not one of those, then it is a script (which may be followed by internal functions).
itsAScript = ~(itsAClass || itsAFunction)
カテゴリ
ヘルプ センター および File Exchange で App Building についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!