Matlab Script Called Before Each Run - matlab

I have some init commands in a m-file on disk. Depending on where the user is executing from, the contents of that init file will will change.
I'm wondering if there is a way to have Matlab automatically call this init script every time a script begins execution (either via the Run key or by requesting a call to a function in the interpreter).
I've looked at the startup.m script and that won't work as it is only executed when Matlab is first started but I want something that runs each time a script begins execution.
I've also thought about placing some boilerplate code at the top of the script file, but, that doesn't work in cell mode as the script may start execution part way through the script which would skip the boilerplate code.

Related

How to set the execution order of several scripts/functions?

I have coded several scripts and I now want to program a MATLAB script that sets the execution order of the scripts. For example, my first script only includes the input variables that the second script requires for calculating certain outputs which are then transferred to a third function and these results are then plotted with the help of the fourth script. Now, my question is: How can I code a script to set the execution order so that I do not have to call each script beforehand?
You should create a fourth script. This script will call all of the scripts in the order you put them there. Matlab interprets a script in the order they are programmed...
script2;
script1;
script3;

Matlab reloading file automatically

Is there any easy way to set a Matlab callback to execute a function whenever a specific file is modified? (I have a C++ program logging data and I want to run a Matlab script over that data everytime I execute the C++ program).
All I saw is that with FileInfo = dir('data.txt') I can get the date and create a script which is testing all the time if the last modification time has changed, but I do not like that solution.

How does MATLAB handle file change while the file is being executed?

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

Monitoring File Changes

Call CreateObject("WScript.Shell").Run(some.exe,0,False)
I'm using that line to call a .exe that returns some text, but can also write it to a file.
I would use .Exec instead of .Run to get the results directly but then the script hangs.
I really don't want a timer checking if the output file is created or modified.
What I need is a way to catch an event somehow. Any Ideas?
Since you mention that it might either return it or write it to a file, does that mean that after the process has written to the file it'll exit?
If so, you could just call the Shell.Run method with the bWaitOnReturn parameter set to True and your script would wait for the process to finish before it continued.
Otherwise, if the process might write a file and then still continue to run, then I think you will have to poll to check if it exists, or possibly you could create a C# or VB.Net exe that uses FileSystemWatcher (or a normal Win32 exe that uses the API FindFirstChangeNotification) to look for the creation of the file and when it finds one it immediately exists and then you could run that process with bWaitOnReturn set to True, but that's probably just overcomplicating things.

.bat script only executes 1 line

I'm writing a script that performs the same function several times, but when I run the script only one of commands executes leaving the rest not executed after the .bat file has run.
Does this have to do with the long time it takes for my commands to run (15-20 sec)? I've written plenty of bat files and I've never run into this. Do I need to have a sleep function between each command?
I've been trying to figure this one out on google, but my available search terms makes my search results vague and difficult.
Any help is definitely appreciated.
the bat file looks something like the following
IF input1 == "search term" goto location
do something
do something
do something
etc
goto end of file
:location
do something else
do something else
do something else
...
Does one of your "do something else" lines involve calling another batch file? If so, do you use the CALL command?
If you want to call another batch file recursively, you need to use CALL. Otherwise, when the called batch file exits, it does not return to the calling batch file and simply exits. This is a relic from the MS-DOS days; since memory was at a premium, the MS developers decided that the batch interpreter shouldn't keep a call stack by default -- so if you wanted one, you had to use CALL.
See call /? for more information.