using inputdlg function in user defined function
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2022-8-7
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...
回答(2 个)
Walter Roberson
2022-8-6
编辑:Walter Roberson
2022-8-7
"I would like to apear the box only one and keep on the simulation run"
That is not possible with inputdlg()
3 个评论
Walter Roberson
2022-8-8
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
Steven Lord
2022-8-8
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Schedule Model Components 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!