Clear Filters
Clear Filters

No block called "Name1" or "Name2" could not be found.

7 views (last 30 days)
Hi all
I created a matlab script that extracts all the block paths and names from a simulink model to save them in an excel file in order to translate them. After the translation I read the excel file and with the same path and based on the old name I`m trying to push the new names to the simulink model. It works, but occasionally it gives me an error regarding a block that the script can`t find, which is allready there, sometimes its name was the latest one that was changed.
Here is the code that I`m using:
%% Get and define names and values from Excel
clc
clear num txt raw;
%% Set translation mode
path_coll = 2;
old_nm_coll = 3;
new_nm_coll = 7; % New english names
%% Get numeric, text and raw data from Excel file
addpath(genpath("Path"));
[num,txt,raw] = xlsread('Excel_name', 'Excel_sheet');
%% Delete first row (header) of each matrix
% num(1,:) = [];
txt(1,:) = [];
raw(1,:) = [];
%% Go through the path list
for i=1:length(txt)
% Process the M.U naming, e.g. ...[kg//h] -> ..[kg/h] and vice-versa
clean_path = strrep(txt{i,path_coll}, '//', '//'); % Path column
raw_name = strrep(txt{i,old_nm_coll}, '//', '/'); % Old name column
% Remove the block name from the full path, thus obtain the root path
root_path = strrep(clean_path, raw_name, '');
% Generate the full path for the new block name
new_path = strcat(root_path, txt{i,new_nm_coll});
if getSimulinkBlockHandle(new_path) ~= -1
disp(['The block "',txt{i,new_nm_coll}, '" is already renamed.']) % "New_Name" collumn
% If block is not renamed yet, rename it and display a message
elseif getSimulinkBlockHandle(clean_path) ~= -1
set_param(txt{i,2},'Name',txt{i,new_nm_coll}); % "New_Name" collumn
disp(['The block "',txt{i,old_nm_coll},'" was renamed to "',txt{i,new_nm_coll},'".'])
else
disp(['No block called "',txt{i,old_nm_coll}, '" or "',txt{i,new_nm_coll},'" could not be found.']) % "Old_Name" collumn
open_system(root_path, 'force'); % Open the subsystem (one level above the block)
end
end
And here is the error that stops all:
The block "Old_name_1" was renamed to "New_name_1".
The block "Old_name_2" is already renamed.
The block "Old_name_3" was renamed to "New_name_3".
No block called "Old_name_3" or "New_name_3" could not be found.
Error using Auto_block_name_corrector (line 40)
Block 'Simulink_path/Old_name_3' not found in block diagram 'Simulink_nodel_name'.
Please help!

Accepted Answer

Manoj Mirge
Manoj Mirge on 24 Feb 2023
Hi Ovidiu,
I am assuming that you have stored the blocks paths and names in excel sheet in the order in which find_system function outputs them. If your Model is named MyModel and it has some blocks, then find_system will output them in below order:
  1. MyModel
  2. MyModel/block1
  3. MyModel/block2
When changing names of blocks in you program if you start changing names in above order then you will encounter errors. Because if you first change the name of first block which is your model's name to MyModel2, then at the time of changing name of second block if you try to change its name using set_param function you will be giving wrong input path to the function.
%Currently you are giving following input to the function:
Set_param("MyModel/block1","Name","NewName");
%But you are supposed to give following input:
Set_param("MyModel2/block1","Name","NewName");
But since your model’s name was changed to MyModel2 the path ‘MyModel/block1’ would be invalid and hence set_param function will generate the error you are getting.
You are going to face same problem if you change the name of subsystem before changing name of its sub blocks.
The solution to your problem would be:
  • Store the block’s paths in excel sheet in opposite order of their order in block diagram hierarchy.
Eg. MyModel/subsytem1/block1
MyModel/subsystem1
MyModel/block2
MyModel
  • This order will ensure that you will never get invalid path since you are changing blocks name in opposite order of their hierarchal order.
  • To sort model’s block in above order, you can make use of number of /’ used by block’s path. More the number of ‘/’ in path would mean the block is in down the hierarchy.
Hope this will help.

More Answers (0)

Categories

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

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!