필터 지우기
필터 지우기

Is it possible to make a while error or while no error Matlab

조회 수: 7 (최근 30일)
Lucas S
Lucas S 2019년 4월 16일
댓글: Lucas S 2019년 4월 16일
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.

답변 (1개)

Ben Cunningham
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.
You can parse errors using 'try - catch' then switch based on any errors generated.
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
  댓글 수: 1
Lucas S
Lucas S 2019년 4월 16일
This can definitively help me but i have to modify all my code so i will think about it. Thanks !

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by