Run one function only once when the function starts everyday or some time period

Hi,
I want to do this: with my login system, I only want it to run once when the user starts to use it everyday or some time let's say 5 hours etc... not everytime when people run the funtion you need to log in every time.
For example this login function is inside one big function, so everytime when you run this big function, it can only run the login once everyday. So even let's say I run the function 3 times within 5 minutes, I do not want to login every time I run. Only the first time is enough....I know a counter might work, like to change from 0 to 1, but then later how can I clear it next day? Or maybe PERSISTENT...anyone can help ?
thanks

2 Comments

I have a rough idea what you want to achieve, but maybe you can give some more details ...? Would make giving a recommendation easier.
Titus
Hi, Thanks Titus. As showed below, i only want to run login function once within one day, when people run Data() function...
function Data()
%only run login function once
function login()
end
code of data processing....etc
end

Sign in to comment.

 Accepted Answer

Yes, use persistent and store the last time it was run in your variable. Then compare the current time to the stored time and if not enough time has elapsed just return from the function:
function login()
persistent rundate;
lastrun = rundate;
rundate = now;
if ~isempy(lastrun) && hours(datetime(rundate, 'ConvertFrom', 'datenum') - datetime(lastrun, 'ConvertFrom', 'datenum')) < 24
return;
end
%... normal content of login
end

7 Comments

Hi,
there is one problem with this:
Undefined function 'datetime' for input arguments of type 'double'.
I think it is soemthing wrongwith the rundate and lastdate format...they should be string . So I changed it to
rundate = 'now';
But still got the problem with char...
There's nothing wrong with the rundate format. They certainly shouldn't be string as computation on strings is not very efficient. The problem is most likely that you are running a version prior to 2014b, where datetime was introduced. datetime makes calculation on dates a lot more robuts.
You can do the computation using the datenum instead:
if ~isempy(lasttime) && rundate - lastrun < 1
In some ways it is simpler, as long as you know how datenum are constructed.
Thanks. But here
persistent rundate lastrun;
lastrun = rundate;
rundate = now;
if ~isempty(lastrun) && ((rundate - lastrun) < 1)
return;
end
I need to return the previous login username and password, not just retuen...because this username and password are parameters to other function...do you know how can I get that? I mean in the GUI user, enter the username and password, first time, Then later no need to input, but those have to be remembered to next round ...
Note that lastrun was not persistent in my code, and does not need to be.
As for your username / password question. It's the first time you mention it, so I've no idea what it's about. The function prototype you showed in your example does not return anything.
If you need to return something, either assign the return values before the check or do it in the body of the if, before the return.
if you need to remember the last login and password, make the variables persistent as well.
Thanks..Got it work now...but...one more small question..(rundate - lastrun) < 1 Here is the time interval 1 day?
Yes, it is in days. As I said you need to understand how datenum are constructed. The integral part is the number of days (since a reference time), the fractional is made up of hours/24 + minutes/(24*60) + seconds/(24*60*60) + milliseconds/(24*60*60*60).
The new datetime and duration types of R2014b make it much easier to extract a duration in whichever unit of time you want.
aha...ok.thanks a lot. this really helps me out.

Sign in to comment.

More Answers (1)

Hi,
you mean something like this:
function Data()
l = login();
code of data processing....etc
function aLogin = login()
persistent theLogin
persistent lastLogin
if isempty(theLogin)
% first call
theLogin = doSomething;
lastLogin = now;
elseif (now-lastLogin) > 2 / 24
% if last login is more than two hours ago
theLogin = doSomething;
end
aLogin = theLogin;
end

Categories

Asked:

on 3 Feb 2015

Commented:

on 4 Feb 2015

Community Treasure Hunt

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

Start Hunting!