using inputdlg function in user defined function

3 views (last 30 days)
MAB
MAB on 6 Aug 2022
Commented: MAB on 9 Aug 2022
Hello
I would like to write this code in matlab function in simulink file
function y = fc()
y=0;
coder.extrinsic('inputdlg');
prompt = {'Enter 1 for Yes 0 for NO'};
dlgtitle = 'Input';
dims = [1 35];
answer = inputdlg(prompt,dlgtitle,dims)
end
when I run the simulation ,the diagloge box apear several time during simulation.
I would like to apear the box only one and keep on the simulation run
could help me to do this?
Another question
I need to add switch case like this
function y = SW_case()
y=0;
coder.extrinsic('inputdlg');
prompt = {'Enter 1 for Yes 0 for NO'};
dlgtitle = 'Input';
dims = [1 35];
answer = inputdlg(prompt,dlgtitle,dims);
switch answer{1}
case 1
y=100
case 0
y=10
end
when I run the simulation
there is an error ocure
Cell contents reference from a non-cell array object.
Function 'MATLAB Function1' (#23.178.187), line 8, column 8:
"answer{1}"
thanks alot in advanced
  2 Comments
dpb
dpb on 7 Aug 2022
I couldn't reproduce the specific error regarding the non-cell object; I got a cell array returned; would seem to need the case in context to reproduce. Then again, I don't have Simulink so I can't simulate calling the function that way.
However, your code isn't testing for the empty return nor that the values are acceptable and the result in the cell array will be a char() string, NOT a numeric value even if the user does "the right thing". This is a tough way to get the input and control what the user does --
...
answer = inputdlg(prompt,dlgtitle,dims);
if isempty(answer)
answer=1; % the default answer???
else
answer=str2double(answer{:}); % convert to number
if isfinite(answer) % could convert the string to a number
if ismember(answer,[0 1]) % make sure either 0,1
else
answer=1; % default if not???
end
end
end
switch answer
case 1
...
is a start but not even close to being fully robust to check alternatives --
I suggest something more like
yn = questdlg('Yes or No?','Input','Yes','No','Yes');
if matches(yn,'Yes')
y=100;
else
y=10;
end
at least controls the possible return values to something you know is one of the two possible button answers although there is also again the possibility the user closes the dialog without selecting either in which case the return is the empty char() vector...the above ignores that possibility and lumps it in with a 'No'
You'll have to decide how to handle all the possibilities but I'd definitely stay away from freeform user input here...they could type in Hamlet's soliloguy in the box for you to parse above -- not that that's likely, but there's no way to limit just typing errors...
MAB
MAB on 8 Aug 2022
Edited: Walter Roberson on 8 Aug 2022
Thanks alot dpb for response
I upload the model (it is a simple example for what I need )
I used the questdlg function and switch case
still error
An error occurred while running the simulation and the simulation was terminated
Caused by:
Undefined function 'check_switch_expression' for input arguments of type 'char'.
if I used num2str function
function y = SW_case1()
coder.extrinsic('questdlg')
coder.extrinsic('str2num')
answer = questdlg('Would you like a dessert?', ...
'Dessert Menu', ...
'1','2','0','0');
a=str2num(answer)
switch a
case 1
y=1.75
case 2
y=3
case 'No thank you'
y=3.5
otherwise
y=3.5+(0.5)
end
still error
An error occurred while running the simulation and the simulation was terminated
Caused by:
Undefined function 'check_switch_expression' for input arguments of type 'double'.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 6 Aug 2022
Edited: Walter Roberson on 7 Aug 2022
"I would like to apear the box only one and keep on the simulation run"
That is not possible with inputdlg()
  3 Comments
Walter Roberson
Walter Roberson on 8 Aug 2022
Tested.
However, this does not leave the prompt box up the way you had hoped for. Also it asks for the value every time step...
function y = SW_case1()
coder.extrinsic('questdlg')
answer = '';
coder.varsize('answer', [1 20], [0 1]);
answer = questdlg('Would you like a dessert?', ...
'Dessert Menu', ...
'Ice cream','Cake','No thank you','Cake');
switch answer
case 'Ice cream'
y=1.75
case 'Cake'
y=3
case 'No thank you'
y=3.5
otherwise
y=3.5+(0.5)
end
MAB
MAB on 9 Aug 2022
Thank you so much Walter Robertson
yes still opening the message every step time
is there any way to do that ? in simulation
I would like to appear a message at if condition is activate
and according to may choose the certain task is occurred

Sign in to comment.


Steven Lord
Steven Lord on 8 Aug 2022
Since you're doing this in a MATLAB Function block, rather than bringing up a dialog each time I'd consider using a Constant block or perhaps one of the Customizable Blocks to define an input to that block.
  1 Comment
MAB
MAB on 8 Aug 2022
Thank you sir for helping me
This code is a part of my code I need to appear The message box after if condition activate and according to the choose of the message box ,the certain task is occurred
I don’t need a constant block or any input to a user defined function
Can I do it?

Sign in to comment.

Categories

Find more on Schedule Model Components 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!