I'm looking to limit the execution of function in Matlab, so if it won't return answer in X seconds the call will be aborted.
I know it's possible with the 2011 Matlab version using timeout, but I've got the 2010 version of Matlab. Is it still possible to limit the execution time of the function?
As others have pointed out you can't do this natively in Matlab. However on Unix systems e.g. Linux or Solaris I have previously used a bit of a dirty hack to achieve the desired effect.
Rather than calling your .m file as a function with parameters, save all the parameter data into a .mat file and write a shell command to invoke Matlab and run your .m file e.g. myfunc.m as a standalone routine e.g.
!bash -c "ulimit -t 3;matlab -nodisplay < myfunc.m"
This would limit myfunc.m to a CPU execution time of 3 second. Note that is CPU not including any disk access etc. There are other flags you can pass to ulimit if you need some other behaviour.
Inside myfunc.m you'd have to save the data myfunc.m wants to return into a .mat file and load it up again in your calling program. Bit of a nasty hack but I have tested it and it works. Note the use of the bash shell for the internal ulimit command.
Related
PROBLEM
I have two tools written in MATLAB (I am not the author):
the first allows me to retrieve some data from a SQL database, but it works only on MATLAB 64bit (I have MATLAB 2016b 64bit).
the second uses the data retrieved from the first tool and, through
a DLL compiled on a 32bit system, it gets some outputs. As said,
this tool works only MATLAB 32bit (I have MATLAB 2013a 32bit).
What I would like to do is:
get the data from SQL, in MATLAB 64bit
"send them" in some way to MATLAB 32bit
run the tool on MATLAB 32bit
"return" the outputs from MATLAB 32bit to MATLAB 64bit
I know a solution may be found using IPC mechanisms, but I am not sure how to find them or how to use them in MATLAB. Does anyone ever worked with this kind of stuff?
Looking into the MATLAB documentation I saw that it is possible to create a COM object, but I am not sure how to use it to run some functions in MATLAB 32bit.
SOLUTION
As suggested by #nekomatic below, everything works for me if I run the code in the way suggested, but excluding -automatic from the system command below.
The final system command is in the form
system('"C:\path\to\R2013a\matlab.exe" -wait -r "mycommand; exit"')
mycommand is a MATLAB script which loads the input file, it does something and then it saves an output file.
If you don't need this operation to be fast, the easiest way to do it is probably:
Save the data from 64-bit MATLAB as a .mat file
Use the system command to start an instance of 32-bit MATLAB
Run a script in 32-bit MATLAB that reads the data from the file, processes it and saves it
Read the result back in to your 64-bit program.
For example the 64-bit code (excluding error handling, current folder setup, etc) might look something like this:
delete result.mat % Delete any result from the previous run
save(data.mat, '-v7.3') % usually best to specify the .mat format to use
system('"C:\path\to\R2013a\matlab.exe" -automation -wait -r "mycommand; exit"')
processedData = load('result.mat')
where mycommand is your MATLAB R2013a script that reads the data from data.mat, processes it, and saves the result in result.mat.
More data on the startup options for R2013a here (assuming Windows) and on the system command for R2016b here. You may need to be signed in with a Mathworks account to see documentation for older releases, but if that's a problem just look in the help in your respective MATLAB installations.
I am trying to run a Matlab script from a tcl file.
There are examples elsewhere of how to do this (eg Running Matlab Command From Tcl) :
exec {*}matlab -nodisplay -nosplash -nodesktop -r "ScriptTitle; quit"
However, as far as I can see, this works by opening up a new Matlab command window before executing the Matlab script.
However, I want to call the Matlab script as part of a loop, meaning it will be called many times by my tcl code. Each time matlab is called, a new matlab command window is opened. This takes a long time as the new command window has to open and load etc before it does anything. I want to know if there is a way to access an already open matlab command window from tcl? Or, alternatively, is there a more efficient way to run Matlab from tcl that would work well as part of a frequently repeated loop?
MATLAB has an API engine which can be used to control it remotely. To use it from Tcl, you can write an extension or use Ffidl.
I'm using MATLAB and calling an .exe via the system command.
[status,cmdout] = system(command_s);
where command_s is a command string that is formatted earlier in my script to pass all the desired options to the .exe. The .exe would normally write to a .csv file via the > redirection operator in Windows/DOS. Instead, this output is going to cmdout where I use it later in the MATLAB script. It is working correctly and as expected. I'm doing it this way so that the process just uses memory and does not write a very large file to the disk, which would then have to be read from the disk and then deleted after I'm done with it. In the end, it saves a .mat file that's usually in hundreds of KB instead of 10s/100s of MBs as the .csv file would be (some unneeded data is thrown out in the end).
The issue I'm having is since I'm dealing with large files, the executable can take a significant amount of time. I typically have to wait about 2 minutes after executing this command. In the meantime, I have no feedback to know it is progressing and that my system hasn't froze. I know I could add the & symbol to the end of my string, command_s, and run MATLAB code while this is running in the background (or asynchronously as some would say), but that brings up an external window AND makes cmdout empty - so I cannot use the output - forcing me to sit there for 2 minutes wondering each time it executes.
Is there any way to run in the background AND get the stdout from the command?
Maybe you could try system(command_s,'-echo')?
I am using Matlab 2014a under Windows7. I am running a loop that reads very big xlsx files (~40MB each). After I am done with a file I use 'clear' in order to free the memory taken by reading the file. The thing is that every once in a while the script is stops and giving me an error message:
Error using xlsread (line 247)
Error: Not enough storage is available to complete this operation.
I want to emphasis that after each time I am finishing with a file I clear all the variables, so each iteration only one file is loaded. If I restart Matlab the script may work again - making me believe that some how 'clear' command doesn't free all the memory that was allocated. is there a way to really free the memory that once was allocated in matlab?
thank you very much
Ariel
If restarting Matlab is not an option, the "pack" function should help. Otherwise you could also use matlab without the gui and write a shell script that starts and matlab for each file.
Assume that you start running the script. What happens when you change that file when it is being executed? It seems that MATLAB takes a copy of the file and then starts executing it. I want to make sure that I am right. That said, I want to run a MATLAB script with different parameters on a clusters. Does it work correctly if I do the changes on that one file. Or do I need to create multiple copies of the file myself?
Changing the contents of a script / function while it is running will not affect the operation of the script as MATLAB is running a (generically speaking) "cached" and "preprocessed" version of the file. As for running a script with multiple parameters in a cluster, I assume you are using the Parallel Computing Toolbox?
One option might be to have the script load its parameters from a MAT file, allowing you to run the same script on all workers, but operate on different parameters.
Basically you will be fine if you only have one Matlab m-file for all of your computation.
But if if the file you edit get called multiple times during your computation then you will run the risk of calling multiple versions of the file by editing while running. See more in here: http://www.mathworks.com.au/matlabcentral/newsreader/view_thread/261376