simple calculator using GUI
조회 수: 266 (최근 30일)
이전 댓글 표시
I tried to make a simple calculator with GUI.
For that purposes i want to take the input as string.then convert this string into number ,and then perfom the operation.
For instance i would like to take input like
("5+6/3")
and then using str2num i want to perform that operation.
In all button i use almost the same code like ( here i show the code for button 1 and divison sign.)
% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject handle to division (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'/'));
% --- Executes on button press in button1.
function button1_Callback(hObject, eventdata, handles)
% hObject handle to button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s=get(handles.screen,'string');
set(handles.screen,'string',strcat(s,'1'));
In equal button
% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject handle to equal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
p = str2num(s); % it will convert the string in mathmatical expression then perform the operation
set(handles.screen,'string',num2str(p));
It didn't show the expected output.
What to change in equal Button?
Thanks in Advance.
댓글 수: 0
채택된 답변
Rik
2021년 4월 19일
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Every callback is a separate function that doesn't know what happened in the other functions. That means the variable s doesn't exist.
function equal_Callback(hObject, eventdata, handles)
s=get(handles.screen,'string');
p = str2num(s); % calls eval implicitly
set(handles.screen,'string',num2str(p));
You should be really careful with str2num. It calls eval, meaning it can execute arbitrary code. You should avoid it when possible. In this case it is safe, as long as the user doesn't type any code in the text box.
추가 답변 (3개)
Ngo Nguyen
2021년 4월 19일
I think you should use the function: eval
댓글 수: 2
Ngo Nguyen
2021년 4월 19일
At first, check your equal_Callback, what is s?
When you start a new function, the parameter "s" is no longer exist?
What kind of the error message did you get?
Md. Ashikur Rahman Any
2021년 4월 21일
댓글 수: 1
Rik
2021년 4월 21일
You should not rely on global variables. You have the handles struct right there to share data between callbacks.
You should also consider storing handles.screen.String separately (and restore it every callback that changes it). That way you can avoid malicious input.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!