Is it possible to make a while error or while no error Matlab
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Ben Cunningham
2019-4-16
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Environment Customization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!