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!