Timer in matlab to repeat segments of code every few minutes?

11 views (last 30 days)
Hi.
I have the following code which downloads a CSV file from a link and then saves it as a formatted file:
while(1)
tic
urlwrite('http://xweb.geos.ed.ac.uk/~weather/jcmb_ws/JCMB_2013_Sep.csv','Weather Data.csv');
data= readtext('Weather Data.csv');
f=2;
disp('done')
T=toc;
pause(10-T)
end
I've used 10-T as 10 seconds just to test the working program. This would actually be around 300-T for the correct value. I'm designing a model which requires me to download this file every 5 minutes.
How can I achieve this as some sort of loop or timer?
The updated data from the file is used for calculations and plotting graphs so I also want those calculations and other parts of my program to be automated every 3 minutes with the change in new file information.
Current code doesn't display any of my workspace variables and continues to loop with 'done' until i hit ctrl+c. Have used 'done' just to check if it was working correctly. This code should be almost like a background task so I still want to see my workspace variables.

Answers (1)

sixwwwwww
sixwwwwww on 31 Oct 2013
Dear P, you can use pause command or cputime or etime according to your need:
See following links for more information:
I hope it helps. Good luck!
  17 Comments
P
P on 1 Nov 2013
I only want to access those variables to check that they are producing the right values. The file they are creating for me will update with the last hour. That file is updated every few minutes. This will be considerably smaller and hence the time to execute should be much shorter. Apologies but some of the jargon you have used has gone over my head a bit since i'm new to MATLAB. Does it seem like my original code may work if this is the case with the new file they are building? I'm going to also want to extract the cell information downloaded from that csv and use it in my calculations on each 5 minute interval. My function readtext.m is formatting the csv file from URLWRITE so that it makes it easier for me to extract it once it's downloaded.
If you could sample or edit some of my code to show how I could get it to download and over-write itself every 5 minutes then that would be appreciated. Otherwise, I really appreciate your time, explanation and assistance!
Cedric
Cedric on 1 Nov 2013
Edited: Cedric on 1 Nov 2013
@ImageAnalyst: you are right, but I got lazy and went on with comments (!)
@P: you are already overwriting the same file. If it is every 5 minutes and only your loop in MATLAB is writing/reading it, there is no real problem. Problems could arise if you needed accesses at a higher frequency and/or if multiple "processes" (whatever they are) would have to access the file. If you are fine having your MATLAB dedicated to this loop, it is fine to implement it as you are doing. There would be an issue if you needed to be able to use MATLAB while it is downloading/processing.
Generally speaking, MATLAB is not the right tool for automatizing tasks like gathering data or serving data to other processes. Hence my comments. If you were designing an important tool which would require a sound design, flexibility, etc, I would advise you to perform the data download/extraction and early processing using e.g. Python to keep it simple enough. The Python "engine/process" could work as a server that could be queried with MATLAB, or, maybe simpler, it could just store data in a database that you would query with MATLAB.
At a MATLAB level, you would build a tool (e.g. with GUI) which doesn't run constantly in a loop, but that you could launch whenever you need it. This tool would allow you to interact with the database (e.g. ask for data availability, get relevant data), and display analysis results.
But all that would required a few weeks of full time work I guess, so it's probably not a solution that you'll wnt to implement.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!