Download stops due to missing data matlab - matlab

I am using websave function of MATLAB to download data from a website. The data is arranged by date. I have put the date in a loop to run the code for a certain period of time. However, on some dates, there is no data available. For those dates, the program halts and give 'internal error'.
I want to know how can I download data without interruptions even if there is missing data on some dates avoiding this internal error.

First of all, that sounds like a very poorly designed API if you're getting internal errors for an empty query result. Maybe there is a better way to make the call that you need? If you show some more code maybe we can help you.
That being said, you can use the try/catch construct to surround the call to websave, that way if it throws an error, it will not interrupt the flow of your program.
for k = date_range
try
websave(filename, url);
catch
% There was some error
disp('Skipping this one!')
end
end

Related

Reading cycle time on a TwinCAT 3 PLC

Is there a way for a PLC program to know its own cycle time?
As a workaround, I can just add a persistent variable or a constant to tell it manually, but that's obviously error prone.
To add to Jakob's answer - you can also use GETCURTASKINDEXEX function (Infosys) instead of GETCURTASKINDEX FB. This way you don't have to instantiate it.
_TaskInfo[GETCURTASKINDEXEX()].CycleTime
Will return cycle time as multiples of 100ns (UDINT)
UDINT_TO_LREAL(_TaskInfo[GETCURTASKINDEXEX()].CycleTime) / 10_000_000
Will return cycle time as seconds (LREAL)
For TwinCAT3 this is available in PlcTaskSystemInfo (variable CycleTime).
Combine it with the FB GETCURTASKINDEX to get the data you want.
See one example (though not cycle-time, but still same FB) here at AllTwinCAT.
This is not directly an answer to your question, but can be used to determine the Cycle time.
I like to use the Time() function. It returns a value of data type TIME. It does not represent an absolute time, but can be used to calculate the time between two calls to Time(). In this way you can calculate the Cycle time.
I use it in function blocks (FB) where timing is critical. In this way the FB knows when it was lastly called instead of assuming that it is being run each scan. Even if I or another user of my FB "forgets" to call the FB each scan, the FB still delivers correct outputs.
You can find info on Time() using this link. There is also a function called LTime() which returns a value of data type LTIME, but it seems that Beckhoff did not bother to document this function.

openbugs generating inital values error

I have a model that I can run in winbugs but I get an error in openbugs when loading and generating initial values even when using the same code and the same data.
When loading initial values in both winbugs and openbugs I get the message that the "chain contains uninitalized variables". Then I can successfully generate initial values in winbugs but in openbugs I get the error "error for node Ny[2] of type GraphBinoimial. Node second argument must be integer valued." My initial values for Ny are Ny=c(4,4,4,4,4,4,4,4). These seem like integers to me. Plus these same initial values are accepted in winbugs.
You may be asking yourself, if I can get it work in winbugs why do I care to get it to work in openbugs? I am most interested in running the whole process through R. I've been trying to do this with R2Winbugs or Brugs (openbugs) but I get errors on initialization with both of these and it even crashes R (I'd never seen the r-bomb before but now I can make it happen repeatedly). I think this is probably for the same reason that I can't run the data manually in openbugs.
Being able to run this through R would greatly reduce the frustration of interacting with win/openbugs especially when running a series of models with different data.
Thanks in advance for your insight.
lg

How to have multiple instances of MATLAB save the same file simultaneously

I am currently writing code to run a series of time-consuming experiments using nodes on a Unix cluster. Each of these experiments takes over 3 days runs on a a 12-core machine. When each experiment is done, I am hoping to have it save some data to a common file.
I have a slight issue in that I submit all of my experiments to the cluster at the same time and so they are likely to be saving to the same file at the same time as well.
I am wondering what will happen when multiple instances of MATLAB try to save the same file at the same time (error/crash/nothing). Whatever the outcome, could I work around it using a try/catch loop as follows:
n_tries = 0;
while n_tries < 10
try
save('common_file',data)
n_tries = 10;
catch
wait_time = 60 * rand;
pause(wait_time);
n_tries = n_tries+1;
end
end
end
Don't.
All Matlab functions are explicitly not safe to use in a multi-threading/processing environment.
If you write to one mat-file simultaneously from multiple matlab sessions, chances are good that either several variables are missing (because e.g. 2 matlab append to the same state of the file) or the whole file gets corrupted.
Save individual files and merge them in a post-processing step.
For such long simulation runs, don't aggregate your data automatically unless you have a reliable framework. There are several reasons:
Out of Memory exceptions or similar while writing can destroy all previous results, this is likely to happen while writing large amounts of data.
Coding errors can destroy previous results. Your code will overwrite at least the most recent added data in case of a collision.
Undetected errors in mex functions, which by randomly hit the matlab address space instead of casing a segmentation fault, can cause Matlab to write crap to your Matfile and destroy previous results.
Use some unique pattern, e.g. pc-name + current date/time
You would be best served by having a single recorder task that does the file output and queue the save information to that task.
Don't forget that the output "file" that you supply to the matlab only has to be file like - i.e. support the necessary methods.

Errordlg for any given Matlab error

I have a question about inner Matlab error management. Right now I have quite a large program with a lot of variables and functions that cumulated over my code writting and I'm 100 percent sure that I did not catch all the bugs and mistakes in the program and I don't want it to crash completely when is used by layman user. So, is there a way to display errordlg message and for example restart the program when there will be any given error directly by Matlab (for example when I forgot to declare a global variable etc.)?
Thanks for answers, Peter
Crashes are good, because they force users to report bugs.
If you don't want to go that route, Matlab provides try-catch: wrap your code in a try-catch block. If there is an error, you'll find yourself in the catch block where you can have Matlab send you an email with the error message, and restart the program if necessary.
You can use try/catch statements to respond to errors in your program. There's more information here.

Idea for doing almost same work in both catch & finally(C#3.0)

I have a requirement.
I am processing some files and after the processing are done I am archiving those files into an archive folder with timestamp appended.
The file archiving and putting time stamp portion I am doing in the Finally block. Now a new requirement has come where I need to mail if something wrong goes in the original files and then I need to archive the same. Now this piece of code I need to handle in the catch block.
But if I write the code entirely in the catch block, then it will fire only if there is an exception; otherwise not.
So basically I am writing the same pice of code in both the catch and finally block.
What is the standard and recommended approach you people think will be better in this case?
I am using C#3.0
Thanks.
A finally block is really for code that "always" has to run, even after an exception. This typically means cleanup type code. It's a "code smell" to see processing type code in a finally block.
Maybe you can post your code, and we can suggest a better way for you.