Timer in matlab to repeat segments of code every few minutes?
Mostrar comentarios más antiguos
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.
Respuestas (1)
sixwwwwww
el 31 de Oct. de 2013
0 votos
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 comentarios
P
el 31 de Oct. de 2013
sixwwwwww
el 31 de Oct. de 2013
You can use while instead with a condition which never becomes false. Since you have to loop again and again. and in this while loop you can use pause command. I think it should work this way.
Cedric
el 31 de Oct. de 2013
P
el 31 de Oct. de 2013
sixwwwwww
el 31 de Oct. de 2013
I tried the following code for testing and it is working smoothly for me:
x = -pi:0.01:pi;
y = sin(x);
figure, plot(x, y)
count = 1;
while 1
tic
count = count + 1
x = -pi:0.01:pi;
y = sin(x);
figure, plot(x, y)
t = toc
pause(5 - t)
end
and also values are visible in the command window and variables are stored and updated in workspace. Are you getting some error or is it just stuck?
Your loop isn't a background job and it's an infinite loop so you never get the command prompt back until you hit ctrl-c. Put a
fprintf('.') ;
in the loop and you'll see points being displayed. Another thing is about your pause: what if T is greater than 10? So you should update the call for something like
pause( max(10-T, 0) ) ;
The best way to create some sort of "background job", if you want to keep being able to use MATLAB a bit during the loop, is to use a timer object.
P
el 31 de Oct. de 2013
sixwwwwww
el 31 de Oct. de 2013
I was running this code in a script not on the command window. See attachment for screen shot. I assumed that you are running the code within script now command window. Try the code in a script then you can see all the information on command window and also workspace variables
P
el 1 de Nov. de 2013
sixwwwwww
el 1 de Nov. de 2013
Can you show your code to find out the problem?
P
el 1 de Nov. de 2013
The whole approach is disputable actually, because:
- You are always overwriting the same file, which makes it difficult to synchronize with whatever should read the data.
- The file that you download is 3.4MB, and it's not a good thing to request it every 10s.
- This is always the same file actually on the server; why do you want to request it over and over?
- Even if it were updated regularly on the server, requesting the whole data from start each time is probably not the way to go. In these situations, we often use APIs provided by the data provider, which allow us to request chunks of data, and not the whole stuff from the beginning each time.
- MATLAB is definitely not well suited for running a background job. Even with the timer, your MATLAB would be stuck each time the file is downloaded. If the download takes 20s and you request it every minute, you have 40s of command prompt use per minute, which makes MATLAB almost unusable.
I am attaching nonetheless two files which will illustrate my point about the timer. You'll see that you'll get the shell back only between downloads. I set a period of 60s, so you can better see it. I also generate file names with a time stamp so there is no problem with open files at this time. Just run P.m.
No way if you want to access them from the shell in parallel with your loop, but you can definitely implement whatever computation you need in the loop (moreover, you have time for that if there is a 5 minutes period). I'd simplify the whole process by getting rid of this local file though. You could URLREAD the CSV, and process the char array rather than URLWRITE it to file and then read the file.
If you have any control on the server side file generation (by talking to people who are generating these files for you), you could ask for file names to be redefined with a time stamp every 10 minutes or every hour, in order to minimize the transfer. On the server side, they could maybe even create a symbolic link to the last file each time they change, so they generate
WeatherData_20131101_1530.csv
WeatherData_20131101_1540.csv
WeatherData_20131101_1550.csv
and always set
WeatherData.csv
to point on the latest version .. this way you don't have to bother about time stamps if you just want the latest file.
Image Analyst
el 1 de Nov. de 2013
Cedric, you should start posting this stuff as your own answer so you get credit for it.
P
el 1 de Nov. de 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.
Categorías
Más información sobre File Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!