Pushback button execution in a while loop

3 views (last 30 days)
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

Answers (1)

Geoff Hayes
Geoff Hayes on 28 Aug 2019
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?

Community Treasure Hunt

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

Start Hunting!