Add and delete Line and port

4 ビュー (過去 30 日間)
Laurensius Christian Danuwinata
Laurensius Christian Danuwinata 2016 年 2 月 15 日
Hello, I have a popup-parameter in a Block Mask. And if an element from the list in popup chosen, the add_line and add_block should be execute. Then, if I choose new element, the old add_line and _block should be deleted and the new add_line and _block will be added. I can use it separately, but how can I write it just in one code? Thanks :D

採用された回答

Arnab Sen
Arnab Sen 2016 年 2 月 22 日
Hi Laurensius,
My understanding from the question is that you would like to perform different action when the popup list element is chosen for the first time than when the list element is chosen for the second time.
We can use 'persistent' variable for this purpose which holds the value between the function calls (Similar to 'static' variable in C). Based on the value of the persistent variable you may take different action as per required.
As an illustration, you may consider the following code in the callback of the particular popup:
b=valueOfPersistent;
if(b==1)
% the add_line and add_block code
disp('b==1');
else
%Code to delete the old add_line and _block and add new add_line and _block
disp(b);
end
And write the function 'valueOfPersistent' as follow:
function b=valueOfPersistent
persistent a;
if isempty(a)
a=1;
end
b=a;
a=a+1;
end
Here the function initializes the variable 'a' only once and increment the values each time the function is called from the callback and returns the latest value.
  2 件のコメント
Laurensius Christian Danuwinata
Laurensius Christian Danuwinata 2016 年 2 月 24 日
Hi Arnab,
it worked actually, if b==1 then add_line, however if b==2.....,n it should delete_line as well as add the new line and port. here is the code : br=ValueOfPersistent
if(br==1) % the add_line and add_block code
for i=(d-numel(a))+1:d
add_block(strcat(gcs,'/DebugCAN'), strcat(gcs,'/',CANBO(room).BOname,'/In1'), 'Name', CANstruct(i).SignalName,'Position',[15 (158+40*(i-(d-numel(a)))) 45 (172+40*(i-(d-numel(a))))])
add_line(strcat(gcs,'/',CANBO(room).BOname),strcat(CANstruct(i).SignalName,'/','1'),strcat('CAN bit-packing 1','/',num2str(i-(d-numel(a)))),'autorouting','on');
end
disp('br==1'); else
%Code to delete the old add_line and _block and add new add_line and _block
for i=(d-numel(a)+1):d
delete_line(gcs,strcat(CANstruct(i).SignalName,'/','1'),strcat('CAN bit-packing 1','/',num2str(i-(d-numel(a)))));
end
delete_block(get_param(find_system(gcs,'regexp','on','LookUnderMasks','all','blocktype','port'),'Handle'))
disp(br); end
It wasn't working for the delete add_line and _block though, do you have any further suggestion?
Laurensius Christian Danuwinata
Laurensius Christian Danuwinata 2016 年 2 月 24 日
shortly, below %code to delete........ In the second loop, it should delete the value from the 1.loop( in 1.loop the line and block will be added) and then add_line and _block with the value from the current loop(2.Loop) and so on. I try to figure it out currently, would be best, if you have any suggestion for it :D, thank you so much anyway for the first answer with persistent function

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by