Pushback button execution in a while loop
조회 수: 4 (최근 30일)
이전 댓글 표시
I am making a continuous measurement using while loop. But I am not saving all the measurements. I have push back button, which adds the measurement to a variable, when the user clicks on it. It should not add the measurement in the next loop to the variable. This is my intention.
How its actually working right now:
The button becomes active after first click, and adds all the measurement to the variable continuously until the end of loop..
load.x is the measurement.
function add_measure_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to add_measure (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.add_meas=1;
guidata(hObject, handles);
while i<100
hObject = h.figureViz;
handles = guidata(hObject);
if handles.add_meas==1
handles.add_meas=0;
X_coord_new = [X_coord_new, load.x]
end
end
댓글 수: 0
답변 (1개)
Geoff Hayes
2019년 8월 28일
Ram - in the body of your add_measure_ButtonDownFcn you do
handles.add_meas=1;
guidata(hObject, handles);
where you initialize add_meas to one and then update the handles structure. The while loop code is
while i<100
hObject = h.figureViz;
handles = guidata(hObject);
if handles.add_meas==1
handles.add_meas=0;
X_coord_new = [X_coord_new, load.x]
end
end
where you get the latest version of the handles structure and check to see if add_meas is one. Since it is (because you have set this previously) you enter the body of the if and set add_meas field to zero...but you don't save the updated handles structure. So on the next iteration of the loop, you get the latest/saved handles structure which still has add_meas set to 1. You need to save the updated structure as
if handles.add_meas==1
handles.add_meas=0;
guidata(hObject, handles);
X_coord_new = [X_coord_new, load.x]
end
so that you don't update the X_coord_new on the next iteration of the loop.
I'm assuming that you have only shown on subset of your code for this function as it isn't clear how i gets updated on each iteration of the loop. When would you exit this loop?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Clocks and Timers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!