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

% --- 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.

 Akzeptierte Antwort

Voss
Voss am 7 Jan. 2023
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 Kommentare

Operator '+' is not supported for operands of type 'cell'.
It gave this error
Voss
Voss am 7 Jan. 2023
Bearbeitet: Voss am 7 Jan. 2023
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange

Gefragt:

Ali
am 7 Jan. 2023

Bearbeitet:

am 7 Jan. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by