set_param and callback argument

Hello,
I have a syntax question. I would like to use the set_param function to define a callback Startfcn of a simulink block. How can I put an argument to my Startfcn function ?
For example : modelName = 'sim_model.mdl' function_name = 'start_function' set_param(modelName,'StartFcn',function_name);
For example, my Startfcn is like this :
function start_function(abc) display(abc) end
Thnak you in advance.

 채택된 답변

TAB
TAB 2011년 10월 24일

0 개 추천

Where you want to put 'StartFcn' callback function, either for a block or for a whole simulink model ?
For StartFcn of a block:
Get the block handle and use with set_param(). For example
bh=gcbh; % Returns handle of current selected block
set_param(bh,'StartFcn',function_name);
For StartFunc of whole model:
Get the model handle and use with set_param(). For example
open_system(modelName);
mh=get_param(modelName_without_extension,'handle'); % Retuns model handle
set_param(mh,'StartFcn',function_name);
-------------
[ EDITED ]
abc_str=sprintf('%f',abc) % Get the value of abc in string
func_str=strcat('start_function(',abc_str,')');
set_param(mh,'StartFcn',func_str);
You can format string 'abc_str' as per data type of variable 'abc' using %d, %s etc.

추가 답변 (1개)

Christophe
Christophe 2011년 10월 24일

0 개 추천

Thank you for your reply.
I am able to use set_param if the startfcn does not have any argument. I would like to know how to do so if I have some input arguments like I wrote in my previous post :
function start_function(abc)
display(abc)
end
How can I put the 'abc' argument in the Startfcn callback, I would be glad if you could help me. I have tried :
set_param(modelName,'StartFcn',{@start_function,abc});
It does not work.
Thank you.

댓글 수: 5

TAB
TAB 2011년 10월 24일
Your question is not clear to me.
You can write string 'start_function(abc)' to StartFcn directly by
set_param(modelName,'StartFcn','start_function(abc)'). When you will run the model argment abc will be resolved from base workspace.
Or are you want to pass the *value of abc* to start_function from the set_param() directly ?
Christophe
Christophe 2011년 10월 25일
Sorry for my unclear question.
Yes I would like to pass the value of abc.
For example, I calculate abc in the main and want to pass this value to the Startfcn function.
I can get my value by passing through workspace but I thought it could be directly done (without using the workspace)
TAB
TAB 2011년 10월 25일
Please see edited part of my previous answer.
Christophe
Christophe 2011년 10월 25일
Thank you but my abc data is a structure. Sorry not to say that at first.
TAB
TAB 2011년 10월 25일
I have to again guess how your structure will be.
Please give some datails. If you can not post original data, express it with examples.

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

카테고리

도움말 센터File Exchange에서 Model, Block, and Port Callbacks에 대해 자세히 알아보기

질문:

2011년 10월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by