Using Date Picker in Timer Object
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I just recently started working with App Designer less than a month ago, so I am relatively new to this. My question is how would you include a Date Picker in a Timer? The code for the timer is already written, but I was asked to update it to where if we wanted the code to run, we could get it to start on a certain day (Date Picker) at a certain time. As for getting it to start at a certain time, I currently have an edit field where the user can input a number to indicate a delay time (in hours) with 12 am being at 0 (that's probably something I also have to fix). If someone could give any ideas, that would be greatly appreciated.
% create a timer
app.t.UserData=[deprivTime,randNum];
app.t.Period = period;
app.t.ExecutionMode = 'fixedRate';
app.t.StartDelay = app.SelectDateDatePicker.Value + (app.StartdelayinhoursEditField.Value * 3600); % include date picker here???
app.t.StartFcn = @SNAPstep; % @SNAPstep means it's directed to the function SNAPstep
app.t.TimerFcn = @SNAPstep;
app.t.StopFcn = @SNAPstep;
app.t.ErrorFcn = @SNAPstep;
app.numExecutions = (app.LengthofexperimentinhoursEditField.Value * 60) / 3;
app.t.TasksToExecute = app.numExecutions;
function SNAPstep(mTimer,event) % has to be a nested function
event_type = event.Type;
switch event_type % fix this to coordinate to date picker
case 'StartFcn' % is this where the date picker should go????
writeline(serialdev,"Q"); % sets the motor to get ready for deprivation/"off position"
disp([event_type ' executed ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
case 'TimerFcn'
disp([event_type ' executed ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
pause(app.t.UserData(2)) % pauses for the specified random interval until the deprivation movement should start
disp(['Deprive executed ',datestr(clock,'dd-mmm-yyyy HH:MM:SS.FFF')]);
writeline(serialdev,"1") % commands the motor to begin depriving the flies
pause(mTimer.UserData(1)) % pauses the function until the deprivator is finished cycling
writeline(serialdev,"Q"); % resets the motor to get it ready for the next deprivation command
app.t.UserData(2)=randi([0,2.25*60],1); %sets a new random interval within the 3 minute window for the next deprivation
case 'StopFcn'
disp([event.Type ' executed ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
stop(mTimer)
case 'ErrorFcn'
disp([event.Type,datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
end
end
3 Commenti
Mario Malic
il 6 Ago 2022
Modificato: Mario Malic
il 6 Ago 2022
app.SelectDateDatePicker.Value is a datetime type, so adding number 1, will add 1 day to it. You add miliseconds afterwards, so it won't do what you expect it to do.
app.t.StartDelay = app.SelectDateDatePicker.Value + (app.StartdelayinhoursEditField.Value * 3600); % include date picker here???
Maybe you should test this behavior inside MATLAB before testing it on the device itself.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Develop uifigure-Based Apps 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!