If else Statement in GUI

조회 수: 15 (최근 30일)
Muhammad Salman Arif
Muhammad Salman Arif 2019년 9월 4일
댓글: Muhammad Salman Arif 2019년 9월 5일
I have been using if-else statement in GUI. My program doesnot executes for 'else' i.e. it never goes for else regardless my if statement is false any one can guide??
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=str2double(get(handles.edit2,'string')); %%upper bound
b=str2double(get(handles.edit3,'string')); %%lower bound
d=str2double(get(handles.edit4,'string')); %%user defined error
c=(a+b)/2; %%mid point
f=get(handles.edit5,'string'); %%getting fntn as string from gui
g=vectorize(f); %% cnvrtng into fntn expression
h=inline(g);
i=h(a) %% fntn value at upper bound
j=h(b) %%fntn at lower bound
if h(a)*h(b)>0
% set(handles.edit1,'string','wrong choice');
else
err=abs(h(c));
while err>d
if h(a)*h(c)<0
b=c;
else
a=c;
end
c=(a+b)/2;
set(handles.edit1,'string',c)
end
end
  댓글 수: 3
Jan
Jan 2019년 9월 4일
There are 2 else commands. Which one do you mean?
Prefer to separate the code for the GUI and the computations. Then you could check the computations easily by providing input arguments.
Setting the contents of the edit1 field does not change the display. Add a drawnow command to give Matlab a chance to update the screen.
Muhammad Salman Arif
Muhammad Salman Arif 2019년 9월 5일
its working good but I need to terminate the program to get the answer. otherwise it keeps on working and show nothing. I have also tried to minimize the accuracy limit in the while loop

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2019년 9월 4일
I guess, that Matlab does exactly what it is expected to do.
If h(a)*h(b)>0 is false, Matlab enters the else branch. Although you set the value of the edit1 field there, the display is not updated, because there is no drawnow command. If you have an infinite loop, it looks like Matlab does nothing, but in is running correctly.
Consider Rik's valuable advice: Use the debugger to step trough the code line by line.
  댓글 수: 1
Muhammad Salman Arif
Muhammad Salman Arif 2019년 9월 5일
edit1 value should only be updated if h(a)*h(b)>0 is true. am I right?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by