MATLAB: GUI toggle button–> while loop operation–> array issue

gui

BIG PICTURE–> I created a GUI for serial port data acquisition, where I use a toggle button to control a while statement for start and stop communication with the port, read data, write to array, plotting to screen, and saving to ascii file.
PROBLEM/ISSUE–> I can not seem to create an array containing the real interation state of the program.
Approach–> I included a nested loop to define the iteration for my data, however, this causes two issues: 1. array has iteration for each data value, but in a repeating fashion based on for statement. 2. including a nested for loop forces toggle button operation to wait until for loop iterations are complete before stopping.
Any thoughts on determining the current iteration state without using nested loops within a while function?
Code and data array output for reference:
% --- Executes on button press in togglebutton1_DVM.
function togglebutton1_DVM_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1_DVM (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
savetxt='dvrtout.txt';
% Hint: get(hObject,'Value') returns toggle state of togglebutton1_DVM
while get(hObject,'Value')
for i= 1 : 9;
tic; pause(0.5);
[DEVHANDLE, ERROR_in_OPEN] = calllib('A2D','Ser1chOpen', 1, 1, 1);
[RETURN_GD, DATA_COUNTS, TIME_SEC_since_1970, ERROR_in_GD]= calllib('A2D', 'Ser1chGetData', DEVHANDLE, 1,1,1,1);
ct=double(TIME_SEC_since_1970);volt=(2.96E-7 * ct);time=ERROR_in_GD;
tint=i;
DATA(i,:) = [tint,volt];%vector array of data
[DEVHANDLE, ERROR_in_Close]= calllib('A2D', 'Ser1chClose', DEVHANDLE,1);
tt=num2str(tint);vv=num2str(volt);
set(handles.CPUTIME,'string',tt);
set(handles.volts,'string',vv);
save(savetxt, 'DATA','-ascii');
toc; it_toc=num2str(toc);
set(handles.iter_toc,'string',it_toc);
end
end
Data output: %iter volts
1.0000000e+000 2.3416149e+000
2.0000000e+000 2.3416128e+000
3.0000000e+000 2.3414973e+000
4.0000000e+000 2.3413393e+000
5.0000000e+000 2.3416465e+000
6.0000000e+000 2.3414751e+000
7.0000000e+000 2.3415376e+000
8.0000000e+000 2.3415400e+000
9.0000000e+000 2.3414171e+000
1.0000000e+001 2.3408429e+000
2.0000000e+000 2.3416128e+000
3.0000000e+000 2.3414973e+000
4.0000000e+000 2.3413393e+000
5.0000000e+000 2.3416465e+000
6.0000000e+000 2.3414751e+000
7.0000000e+000 2.3415376e+000
8.0000000e+000 2.3415400e+000
9.0000000e+000 2.3414171e+000

Best Answer

define a counter before the while statement
counter=1;
while get(hObject,'Value')
tic; pause(0.5);
[DEVHANDLE, ERROR_in_OPEN] = calllib('A2D','Ser1chOpen', 1, 1, 1);
[RETURN_GD, DATA_COUNTS, TIME_SEC_since_1970, ERROR_in_GD]= calllib('A2D', 'Ser1chGetData', DEVHANDLE, 1,1,1,1);
ct=double(TIME_SEC_since_1970);volt=(2.96E-7 * ct);time=ERROR_in_GD;
DATA(c,:) = [tint,volt];%vector array of data
[DEVHANDLE, ERROR_in_Close]= calllib('A2D', 'Ser1chClose', DEVHANDLE,1);
tt=num2str(tint);vv=num2str(volt);
set(handles.CPUTIME,'string',tt);
set(handles.volts,'string',vv);
save(savetxt, 'DATA','-ascii');
toc; it_toc=num2str(toc);
set(handles.iter_toc,'string',it_toc);
c=counter+1
end
Related Question