How can I initiate a while loop with a GUI button (using UI control) and then end that loop with a different GUI button?

3 visualizzazioni (ultimi 30 giorni)
I am working with a tracking system and I have been using a while loop to continuously scan for data. The whole time I have been terminating the loop with control-c, but now I am manipulating an old GUI and trying to initiate and terminate the loop with buttons in the GUI. I thought this might work but I am not having any luck:
%pivot shift button %collects data while subject performs pivot shift PS_data = uicontrol('Position',[1125 250 100 25],'String','Pivot Shift', 'FontSize',12,'Callback','GUI_data_collection'); %calls data collection
%This ends the pivot shift PivotShiftEndButton = uicontrol('Position',[1125 200 100 25],'String','End Shift',..., 'FontSize',12,'Callback','delete(PS_data)');
fprintf(Ftrak,'C'); shift_data = [];
while ishandle(PS_data)
scan = fscanf(Ftrak,'%f');
scan= scan';
if length(scan) == 7
shift_data = [shift_data;scan];
if shift_data(end,1) == shift_data(end - 1,1)
shift_data(end,:) = [];
end
end
end
  1 Commento
Jan
Jan il 1 Ago 2017
Please use the "{} Code" button to format you code, such that it becomes readable. Then explain "not having any luck" with details. It is easier to fix a problem than to guess, what the problem is.

Accedi per commentare.

Risposta accettata

Jan
Jan il 1 Ago 2017
Modificato: Jan il 1 Ago 2017
Do not use a string to define a callback. This is supported for backward compatibility with the ancient Matlab 5.3 only. Use a function handle instead:
...'Callback', @GUI_data_collection
'delete(PS_data)' is a bad callback also: Remember that callbacks, which are defined as strings, are evaluated in the base workspace. There PS_data is unknown. Better use a flag stored in the GUI:
PivotShiftEndButton = uicontrol('Position',[1125 200 100 25],'String','End Shift',...,
'Callback', @StopDataCollection, ...
'UserData' 1); % <-- The flag
fprintf(Ftrak,'C');
shift_data = [];
while get(PivotShiftEndButton, 'UserData')
scan = fscanf(Ftrak,'%f').';
if length(scan) == 7
shift_data = [shift_data;scan];
if shift_data(end,1) == shift_data(end - 1,1)
shift_data(end,:) = [];
end
end
drawnow; % Give the GUI a chance to update
end
function StopDataCollection(hObject, EventData)
set(hObject, 'UserData', 0);
end
Note that the iterative growing of the array uses a huge amount of resources. In each iteration 7 elements a 8 bytes are appended. Therefore a new array is created an the old values are copied. for 10'000 iterations, this does not allocate 10'000*56 bytes=560kB, but sum(1:10000) * 56 bytes: 2.8 GB!
Better pre-allocate the output with a maximum number of recorded data. Although such a limit is a drawback, the current method is limited by exhausting the computer until it crashs.
  5 Commenti
Joseph
Joseph il 4 Ago 2017
My Matlab has been down for a while in my lab so I am just now coming back to the problem sorry for the late reply. When I go to collect data again after hitting "End Shift" the while loop does not initiate again and data is not collected. I am going to assume that this is because "UserData" is now 0 instead of 1. How should I fix this?
Thank you so much
Jan
Jan il 5 Ago 2017
You can fix the value of the UserData by setting them:
set(PivotShiftEndButton, 'UsersData', 1);
I do not know where this must be inserted, because I do not know the complete code for this GUI. Perhaps before the loop or after it, maybe in a callback.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by