How to interrupt loop plotting using a callback on a custom app?
Mostra commenti meno recenti
Hi
I'm currently working on an app that allows me to connect to a remote machine, send some settings, read a signal data back, and plot that data on two axes objects. I already achieved to plot a single shot, using a button, so every time a got a change on the settings, I can qeue a new shot to the server.

I would like to be able to ask the app to run in kind of a loop, in order to get closer to a real time plotting (not necesarelly real time, just close to it) using the "Run" switch that is located on the "Data Reading Settings" panel.
It has a data change callback that sets the app on "Busy Mode"
function RunSwitchValueChanged(app, event)
if strcmp(app.RunSwitch.Value, 'On')
app.RecieveData = 1 ;
app.PlotButton.Enable = 'off' ;
app.PlotButton.Text = 'Plotting';
app.BatchRouteEditField.Editable = 'off';
app.ServerIPEditField.Editable = 'off';
app.SocketServerPortEditField.Editable = 'off';
app.OnGoingLamp.Color = [0 0 1] ;
AutoUpdatePlot(app)
else
app.RecieveData = 0 ;
app.PlotButton.Enable = 'on' ;
app.PlotButton.Text = 'Plot';
app.BatchRouteEditField.Editable = 'on';
app.ServerIPEditField.Editable = 'on';
app.SocketServerPortEditField.Editable = 'on';
app.OnGoingLamp.Color = [0.5 0.5 0.5] ;
end
end
So I tried using a function (AutoUpdatePlot) to run a loop and check for the switch value, and when this value changes, break that cycle and get back to "Manual Shot Plotting Mode"
function AutoUpdatePlot(app)
while strcmp(app.RunSwitch.Value, 'On')
app.trigger_edge_currentIndex = uint32(str2double(app.TriggerEdgeSwitch.Value));
app.trigger_mask_currentIndex = uint32(app.TriggerMaskEditField.Value);
app.trigger_level_currentIndex = uint32(app.TriggerLevelEditField.Value);
app.samp_b_trigger_currentIndex = uint32(app.SamplesBeforeTriggerKnob.Value);
app.total_samples_currentIndex = uint32(str2double(app.TotalSamplesKnob.Value));
app.dec_factor_currentIndex = uint32(app.DecFactorEditField.Value);
app.start_osc_currentIndex = uint32(2);
app.start_system_currentIndex = uint32(app.TriggerAdjustKnob.Value);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DATA READING SETTINGS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
shift_variable_trigger_edge = uint32(1);
shift_variable_trigger_mask = uint32(2);
shift_variable_trigger_level = uint32(3);
shift_variable_samp_b_trigger = uint32(4);
shift_variable_total_samples = uint32(5);
shift_variable_dec_factor = uint32(6);
shift_variable_start_osc = uint32(7);
shift_variable_start_system = uint32(8);
data_size=(double(app.total_samples_currentIndex))/3;
variable_trigger_edge = bitor(bitsll(shift_variable_trigger_edge,28),app.trigger_edge_currentIndex);
variable_trigger_mask = bitor(bitsll(shift_variable_trigger_mask,28),app.trigger_mask_currentIndex);
variable_trigger_level = bitor(bitsll(shift_variable_trigger_level,28),app.trigger_level_currentIndex);
variable_trigger_sbt = bitor(bitsll(shift_variable_samp_b_trigger,28),app.samp_b_trigger_currentIndex);
variable_samples = bitor(bitsll(shift_variable_total_samples,28),app.total_samples_currentIndex);
variable_dec_factor = bitor(bitsll(shift_variable_dec_factor,28),app.dec_factor_currentIndex);
variable_start_osc = bitor(bitsll(shift_variable_start_osc,28),app.start_osc_currentIndex);
variable_start_system = bitor(bitsll(shift_variable_start_system,28),app.start_system_currentIndex);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WRITE SETTINGS INTO REMOTE SERVER %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fopen(app.tcpipObj);
flushinput(app.tcpipObj);
flushoutput(app.tcpipObj);
fwrite(app.tcpipObj,variable_trigger_edge,'uint32','sync');
fwrite(app.tcpipObj,variable_trigger_mask,'uint32','sync');
fwrite(app.tcpipObj,variable_trigger_level,'uint32','sync');
fwrite(app.tcpipObj,variable_trigger_sbt,'uint32','sync');
fwrite(app.tcpipObj,variable_samples,'uint32','sync');
fwrite(app.tcpipObj,variable_dec_factor,'uint32','sync');
fwrite(app.tcpipObj,variable_start_system,'uint32','sync');
fwrite(app.tcpipObj,variable_start_osc,'uint32','sync');
fwrite(app.tcpipObj,bitsll(uint32(0),28),'uint32','sync');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SOCKET SERVER DATA READING AND PLOTTING %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_size_2 = 3*data_size;
data=fread(app.tcpipObj,(data_size_2),'int16');
data_ch1=data(2:2:end);
data_ch1=((data_ch1)*(3.70E-3))+0.379691737;
data_ch2=data(1:2:end);
data_ch2=(data_ch2*(3.71E-3))+0.313208036;
fclose(app.tcpipObj);
%%%%%%%%%%%%%%%%%%%%%%%%% CH1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(app.channel_1,data_ch1(app.samp_b_trigger_currentIndex+1:end),'y');
xlim(app.channel_1,[0,data_size]);
%%%%%%%%%%%%%%%%%%%%%%%% CH2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(app.channel_2,data_ch2(app.samp_b_trigger_currentIndex+1:end),'g');
xlim(app.channel_2,[0,data_size]);
flushinput(app.tcpipObj);
flushoutput(app.tcpipObj);
pause(0.01);
if strcmp(app.RunSwitch.Value, 'Off')
break
end
end
end
The issue here is that when running the app like this, and turning off the Run switch, the app does nothing, and keeps on the loop, for a long while, then, it kind of reacts and does what is intended to do, and also, when the loop is running the app doesn't gets the settings changes, event when I'm using a DataChange callback for all the settings:
% Value changed function: BatchRouteEditField,
% DecFactorEditField, SamplesBeforeTriggerKnob,
% ServerIPEditField, SocketServerPortEditField,
% TotalSamplesKnob, TriggerAdjustKnob, TriggerEdgeSwitch,
% TriggerLevelEditField, TriggerMaskEditField
function InputValueChanged(app, event)
if app.RecieveData == 1
app.trigger_edge_currentIndex = uint32(str2double(app.TriggerEdgeSwitch.Value));
app.trigger_mask_currentIndex = uint32(app.TriggerMaskEditField.Value);
app.trigger_level_currentIndex = uint32(app.TriggerLevelEditField.Value);
app.samp_b_trigger_currentIndex = uint32(app.SamplesBeforeTriggerKnob.Value);
app.total_samples_currentIndex = uint32(str2double(app.TotalSamplesKnob.Value));
app.dec_factor_currentIndex = uint32(app.DecFactorEditField.Value);
app.start_osc_currentIndex = uint32(2);
app.start_system_currentIndex = uint32(app.TriggerAdjustKnob.Value);
updatePlot(app)
end
end
So then I'd like to know, How do I get to cancel the loop the moment I turn off the switch? and How do I get the settings changes get into the loop when it is running?
I'm attaching the app code, If anyone wants to add some info, or any kind of advice, you are welcome :)
Thanks in advance
My best regards
Jesus Lopez
Risposte (0)
Categorie
Scopri di più su Develop Apps Using App Designer in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!