Timer in matlab to repeat segments of code every few minutes?
6 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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
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.
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!