Using Date Picker in Timer Object

5 次查看(过去 30 天)
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 个评论
Chloe Soriano
Chloe Soriano 2022-8-5
What if I put it in the "create a timer" section by citing it as a StartDelay? As in:
% defining time variables
period = 3 * 60; % interval between timer task executions (defined as 3 min)
deprivTime = 45; % seconds it takes for deprivator to oscillate 3 times (with some time cushion)
randNum = randi([0,2.25*60],1); % produces a random number for oscilation start
% connect to the deprivator
v = visadev("ASRL3::INSTR");
% 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(v,"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(v,"1") % commands the motor to begin depriving the flies
pause(mTimer.UserData(1)) % pauses the function until the deprivator is finished cycling
writeline(v,"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
Mario Malic
Mario Malic 2022-8-6
编辑:Mario Malic 2022-8-6
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by