How to get .m file definition programmatically?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Sulaymon Eshkabilov
am 20 Feb. 2023
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
am 20 Feb. 2023
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)
0 Kommentare
Siehe auch
Kategorien
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!