Start/stop button Matlab App

I have an app that outputs a predetermined set of data in the background and plots the inputs at the same time. The program begins when I press the start button. The 'start' button then becomes a 'stop' button. I want the button to display 'start' again when all the data has been outputted. How do I do this?
I thought about pausing for the amount of time that the data takes to run, but that would that stop the program from plotting?
% Button pushed function: STARTButton
function STARTButtonPushed(app, event)
if app.ButtonState==0
app.FileName = [num2str(app.LowV.Value),'-',num2str(app.HighV.Value), 'akV_',num2str(app.AMTFrequency.Value), 'Hz_', num2str(app.Fill.Value), 'ml',app.Filename.Value];
app.ButtonState=1;
app.STARTButton.Text = 'Stop';
app.File=fopen(app.FileName,'a');
fprintf(app.File,'%s\n','Time[s] Low voltage[kV] HASEL[kV] High voltage[kV] Force[N] Displacement[mm]');
% Charging
% trek voltage signals
lowV = (app.LowV.Value*1000/app.Amplification)*ones(5*app.SamplingRate,1);
highV = (app.HighV.Value*1000/app.Amplification)*ones(7*app.SamplingRate,1);
noV = 0*ones(app.SamplingRate,1);
trekV = [lowV; highV; noV];
% relay 1
Relay1on = 5*ones(4*app.SamplingRate,1);
Relay1off = 0*ones(9*app.SamplingRate,1);
Relay1 = [Relay1on; Relay1off];
% relay 2
Relay2on = 5*ones(11*app.SamplingRate,1);
Relay2off = 0*ones(2*app.SamplingRate,1);
Relay2 = [Relay2on; Relay2off];
% AMT signal
app.AMTtime = linspace(0,app.Cycles.Value/app.AMTFrequency.Value,app.SamplingRate*app.Cycles.Value/app.AMTFrequency.Value)';
AMTstart = app.MinForce.Value*ones(size(Relay2));
amplitude = (app.MinForce.Value - app.MaxForce.Value)/2;
offset = (app.MinForce.Value + app.MaxForce.Value)/2;
AMTon = (offset + amplitude*cos(2*pi*app.AMTFrequency.Value*app.AMTtime))/app.ForceConversion;
AMT = [AMTstart; AMTon];
Relay1 = [Relay1; zeros(size(AMTon))];
Relay2 = [Relay2; zeros(size(AMTon))];
trekV = [trekV; zeros(size(AMTon))];
queueOutputData(app.Session, [trekV,Relay1,Relay2,AMT]);
% zero everything
queueOutputData(app.Session, zeros(app.SamplingRate,4));
startBackground(app.Session);
%once this data has been outputted then I want the button to say 'stop'
else
app.ButtonState=0;
app.STARTButton.Text = 'Start';
stop(app.session)
% discharge capacitors
Relay1 = 5*ones(app.SamplingRate,1);
Relay2 = Relay1;
trekV = zeros(app.SamplingRate,1);
AMT = trekV;
queueOutputData(app.Session, [trekV,Relay1,Relay2,AMT]);
startBackground(app.session)
pause(2)
stop(app.Session);
pause(2)
fclose(app.File);
% delete(lh1)
end

1 commentaire

Thomas Satterly
Thomas Satterly le 13 Nov 2019
I think you either need some way of determining the state of your background application or pass along a callback method in the "startBackground" call that's called once it's finished. I would recommend a callback method, as continuously checking for the state from the caller is wasteful.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur App Building dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by