Search model for absolute path references

4 次查看(过去 30 天)
I am trying to find the grep equivalent for searching through models for absolute paths that might be set in a GUI box inside of a simulink model. This is apart of our distribution checklist to ensure other folks can use our models. We currently run all of our models out a simulink project but that doesn't ensure that absolute paths are non existent. The only method we have currently is running the model on another system and seeing if it fails to load.
Here is my current attempt but I am unable to find an absolute path set in Model Explorer / Model Workspace / Workspace Data / File Name gui. I can actually grep the .mdl file and find the absolute path saved in the model but i'd prefer a matlab script solution.
function checkForPaths(model)
load_system(model)
BlockPaths = find_system(model,'Type','Block');
for i = 1 : length(BlockPaths)
BlockDialogParameters = get_param(BlockPaths{i}, 'DialogParameters'); % get all dialog parameters for this block
ParamNames = fieldnames(BlockDialogParameters);
% iterate through the parameter values to check if any are an absolute path
for j = 1 : length(BlockDialogParameters)
paramVal = get_param(BlockPaths{i}, ParamNames{j});
% check if paramVal is an absolute path in the list from Dependency report here
assert(~isLegalPath(paramVal))
end
end
end
function bool = isLegalPath(string)
exists = java.io.File(string).exists();
isDirectory = java.io.File(string).isDirectory();
isFile = java.io.File(string).isFile();
if any([exists, isDirectory, isFile])
bool = java.nio.file.Paths.get(string).isAbsolute();
else
bool = false;
end
end
  1 个评论
Sachin
Sachin 2023-3-14
Could you please provide more information about absolute path saved in the model ? Or you just want the absolute path of your model ?

请先登录,再进行评论。

回答(1 个)

Shubham
Shubham 2023-4-7
Hi Hans,
You can use the find_system function in Simulink to search for all blocks in your model, and then use the get_param function to retrieve the parameter values for each block. You can then check if any of the parameter values are absolute paths using the isLegalPath function that you defined.
Here's an example of how you could modify your checkForPaths function to search for absolute paths in GUI boxes:
function checkForPaths(model)
load_system(model)
% Search for all blocks in the model
BlockPaths = find_system(model,'Type','Block');
% Iterate through all blocks
for i = 1 : length(BlockPaths)
% Get all dialog parameters for this block
BlockDialogParameters = get_param(BlockPaths{i}, 'DialogParameters');
% Iterate through all parameter names for this block
ParamNames = fieldnames(BlockDialogParameters);
for j = 1 : length(ParamNames)
% Get the parameter value for this block and parameter name
paramVal = get_param(BlockPaths{i}, ParamNames{j});
% Check if the parameter value is an absolute path
if isLegalPath(paramVal)
disp(['Absolute path found in block ' BlockPaths{i} ', parameter ' ParamNames{j} ': ' paramVal]);
end
end
end
end
Note that in the modified function, we added a disp call to print out the absolute paths that are found in the GUI boxes. You can modify this part to do whatever you want to do with the absolute paths.
Also note that the isLegalPath function you defined checks if a given string is a legal path on the system using the java.io.File class. This should work for most cases, but you may want to modify it to suit your specific needs.

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by