When I press ok button it automatically goes to command window.

7 ビュー (過去 30 日間)
Ali
Ali 2023 年 1 月 7 日
編集済み: Voss 2023 年 1 月 7 日
% --- Executes on button press in ok.
function ok_Callback(hObject, eventdata, handles)
% hObject handle to ok (see GCBO)
set(handles.text11,'String','');
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str=get(handles.inputastext,'String');
money=str2num(str);
str=get(handles.text11,'String');
c = get(handles.buttongroup,'SelectedObject');
sosSelection = get(c,'String');
switch sosSelection
case 'Espresso'
handles.espressoPrice = 2.00;
while money<handles.espressoPrice %to calculate price of espresso
change_e=handles.espressoPrice-money
ve=(strcat(str,(['You need add (',num2str(change_e),' euros): '])));
set(handles.text11,'String',ve);
money=money+input(ve);
if money==handles.espressoPrice
de=(strcat(str,(['Enjoy your coffee'])));
set(handles.text11,'String',de);
else money>handles.espressoPrice
left_e=money-handles.espressoPrice
re=(strcat(str,(['Here your (',num2str(left_e),' euros). ENJOY YOUR COFFEE'])))
set(handles.inputastext,'String','');
end
end
I want to do anything that the program will continue in GUIDE not command window.

採用された回答

Voss
Voss 2023 年 1 月 7 日
Control switches to the command line because of your use of the input function here:
money=money+input(ve);
If you use inputdlg instead, then a dialog box will appear where the user can input the required information, rather than entering it on the command line.
  2 件のコメント
Ali
Ali 2023 年 1 月 7 日
Operator '+' is not supported for operands of type 'cell'.
It gave this error
Voss
Voss 2023 年 1 月 7 日
編集済み: Voss 2023 年 1 月 7 日
The inputdlg documentation page I shared tells you what inputdlg outputs (it's a cell array).
Here's the relevant section:
Here's an outline of how you might use it (realize that any time your program is taking user input, whether through input or inputdlg or otherwise, you'll need some logic that makes sure they gave you something you can use; without such logic your program is prone to errors and/or unexpected behavior):
result = inputdlg('Give me more money!')
if isempty(result) % user clicked Cancel or closed the dialog box without clicking OK
% you figure out what you want to do in this case
else
% convert the user-entered character vector to a number:
result = str2double(result{1});
if isnan(result) % user entered something that could not be converted to a scalar number,
% e.g. '2 3' or 'baba' or '' (empty character vector - user didn't enter anything), etc.
% you figure out what you want to do in this case
else
% user entered a good value (scalar number) you can use -> use it
money = money + result;
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by