Comparison of M-files and Simulink models
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Hitesh
2025년 5월 8일
편집: Hitesh
2025년 5월 8일
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:
Simulink.findVars: https://www.mathworks.com/help/simulink/slref/simulink.findvars.html
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!