Is it possible to make a while error or while no error Matlab
3 views (last 30 days)
Show older comments
Hello !
In my matlab script i'm parsing a text file with a specific tag structure and creating Simulink Block for each occurence of a special Tag i found. A simple example i have this :
[Link]
Link_Name : On/Off
Link_ID : _sZfSkku9Eemg_bhrv2HEbw
[Link]
Link_Name : On/Off
Link_ID : _qsYbsVeeEemna8dVWPKMTw
You can see that this is not the same Object but they have the same name and so when in matlab i create a simulink block for each Link i found i have an error
"can't create a new On/Off Block"
something like that.
So i put the ID in the description of the Block and if it's the same i just update the name in case of if the user changed the name in the text file :
set_param(gcb,'Name', link_NameValue);
If it's different i create a new Block :
add_block('simulink/Ports & Subsystems/In1',[component_NameValue '/' link_NameValue], 'MakeNameUnique', 'on');
The problem is with " 'MakeNameUnique', 'on' " it will create an infinity of Block if i run my script many times
and with set_param i have an error
The name 'On_Off' already exists
So i would like to make a while loop like this :
while error "can't create a new block"
add a "x" at the end of the name of the new block
end
or
while error "The name 'On_Off' already exists"
add a "x" at the end of the name of the existing block
end
So even if i have 4 [Link] with the name On/Off it will create On/Off, On/Offx, On/Offxx, On/Offxxx or with number at the end if it's possible.
Thanks for helping ! I tried to explain as short as i could.
0 Comments
Answers (1)
Ben Cunningham
on 16 Apr 2019
I don't quite follow the use of 'while' here but if I understand correctly then hopefully the following will be useful.
So something like :
try
% <your parse / create block code>
catch ME
switch ME.message
case "can't create a new block"
% add a "x" at the end of the name of the new block
case "The name 'On_Off' already exists"
% add a "x" at the end of the name of the existing block
otherwise
rethrow(ME)
end
end
See Also
Categories
Find more on Simulink Environment Customization 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!