Comparison of M-files and Simulink models

8 views (last 30 days)
賢斗 森谷
賢斗 森谷 on 6 May 2025
Edited: Hitesh on 8 May 2025
How can I write a script in MATLAB to extract variables that exist in M-files but do not exist in Simulink models? If you have any ideas, please let me know.
  1 Comment
Ayush Aniket
Ayush Aniket on 8 May 2025
Can you share any files and the output that you are expecting?

Sign in to comment.

Answers (1)

Hitesh
Hitesh on 8 May 2025
Edited: Hitesh on 8 May 2025
Hi 賢斗 森谷,
You need to use regular expressions to extract variables from the M-file. However, for Simulink models, the approach depends on the type of model you have and how the variables are defined or stored within it. Once you have both sets of variables, you need to simply use the "setdiff" function to identify which variables exist in the MATLAB M-file but not in the Simulink model.
Kindly follow the steps outlined below to accomplish this:
Extract Variables from M-files
  • Parse the M-file(s) to find variable assignments.
  • One way is to use static code analysis (e.g., regex for assignments).
% 1. Extract variables from M-file
mfile = 'your_M_File.m'; % specify your M-file
fid = fopen(mfile, 'rt');
mtext = fread(fid, '*char')';
fclose(fid);
% Simple regex for variable assignments (improve as needed)
mvars = regexp(mtext, '(^|\n)\s*([A-Za-z][A-Za-z0-9_]*)\s*=', 'tokens');
mvars = unique(cellfun(@(x) x{2}, mvars, 'UniformOutput', false));
Extract Variables from Simulink Model
  • In case of models use "Simulink.findVars" or "Simulink.allBlockDiagrams" to get variables used in the model.
% 2. Extract variables from Simulink model
model = 'your_model'; % specify your model name
load_system(model);
simVarsStruct = Simulink.findVars(model);
simVars = {simVarsStruct.Name};
  • In case of library (not a model), you need to extract variables differently, because libraries are not intended for simulation and often do not have workspace variables in the same way as models.Variables in libraries are typically defined in block masks, mask initialization code, or callbacks.You need to programmatically search for mask variables in all masked blocks or callbacks.
% Load the library
load_system('your_Library');
% Find all masked blocks in the library
maskedBlocks = find_system('LibraryTest', 'Mask', 'on');
simVars = {};
for i = 1:length(maskedBlocks)
maskVars = get_param(maskedBlocks{i}, 'MaskVariables');
% maskVars is a string like 'a=@1; b=@2;'
% Parse variable names
vars = regexp(maskVars, '([A-Za-z]\w*)\s*=', 'tokens');
simVars = [simVars, vars{:}];
end
simVars = unique(simVars);
Compare the Two Sets
  • Find variables present in the M-file but not in the Simulink model.
% 3. Find variables in M-file but not in Simulink model
only_in_mfile = setdiff(mvars, simVars);
% Display results
disp('Variables in M-file but not in Simulink model:');
disp(only_in_mfile);
For more information regarding "regexp","Simulink.findVars","find_system" and "get_param". Kindly refer to the following MATLAB documentation:

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!