How to get .m file definition programmatically?

조회 수: 16 (최근 30일)
Travis Bueter
Travis Bueter 2023년 2월 20일
답변: Image Analyst 2023년 2월 20일
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
Steven Lord
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
Travis Bueter 2023년 2월 20일
편집: Travis Bueter 2023년 2월 20일
I am working to create a system to do a "health check" of our repositories that is performed by a CI system. Some of the checks include ensuring files are in the correct location (e.g. no scripts in certain folders, classes always in an @ folder when in a package, etc.) and other supplementary files exist (e.g. documentation, test files, etc.), which differ between classes and functions.
Currently I don't need to know if a script contains a local function.

댓글을 달려면 로그인하십시오.

답변 (2개)

Sulaymon Eshkabilov
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)
  댓글 수: 1
Travis Bueter
Travis Bueter 2023년 2월 20일
Thank you for your response! Distinguishing the files by their extension is not the problem I am looking to solve. I added a comment to help clarify what my goal is: https://www.mathworks.com/matlabcentral/answers/1915745-how-to-get-m-file-definition-programmatically#comment_2624560

댓글을 달려면 로그인하십시오.


Image Analyst
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)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by