No block called "Name1" or "Name2" could not be found.
Show older comments
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
More Answers (0)
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!