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

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

Related

Matlab parfor in different script file "exist" error

My code runs ok if all parfor code is in the same script file, but since the code is huge and I want to select parallel or serial mode execution, I separated it in a different script file, as fallow:
if (useParFor)
myParforCode.m
else
serialCode.m
end
The problem is that Matlab gives me this error:
"Using exist to check for a variable in the transparent workspace is
not supported."
But if I copy all the code in myParforCode.m and put it after the if statement instead of calling the script, the code runs. I thought I could divide my code in scripts without problems, but it seams it not like that.
What are the limitations here, what I'm doing wrong?
My code is huge but I'll try to create a running code sample and add it here.

How to call a function in Matlab when two programs take input data from different directories?

I am trying to read output of one program into another program. But in each of these programs, I am using some input data which is being loaded from those respective directories. For example,
In program 1, I load some text files and run the code for some array of outputs.
In Program 2, I load some other text files and run the program. And in program 2 I also want to use data which is output of program 1.
Text files in program 1 and 2 are at different locations.
Can you suggest me to handle this issue?
As mentioned in the comments, you only need to use the absolute path to your files:
load('C:\...\dataFolder\dataFile.mat')
This way you can reach it independently on the current working folder in MATLAB.

Running MATLAB system command in background with stdout

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')?

Multiple pathdef files in Matlab?

Suppose two different Matlab users share a computer and they each want to be able to save and load their own Matlab paths. (Or, a single user wants to use different paths at different times.) What's the easiest way to handle this?
Should there be multiple pathdef files? Alternatively, should they each have a script that calls restoredefaultpath and then uses addpath to add new paths?
You can use the startup.m file for that.
When starting up, Matlab executes the file matlabrc.m, which is the master startup file, and is common for all users. Among other things, this file
Sets the first entry of the path as the user-folder of the current user, that is, the user that started Matlab. (This is done by calling pathdef, which in turn calls userpath); and then
Looks for a startup.m file in the path, end executes if it exists.
Therefore you can place a user-specific startup.m file in each user's folder, and Matlab will run the appropriate file depending on which user started Matlab. In that file you can set the path on a per-user basis, and do other user-related stuff.

MATLAB doesn't find files I downloaded while the script is running

My problem is as described. My script downloads files through an external call to cmd (using the system function and then .NET to make keypresses). The issue is that when it tries to fopen these files I downloaded (filenames from a text file I write as I download), it doesn't find them, causing an error. When I run the script again after seeing it fail, it works but only up to the point where it's trying to download/call new files again, where it runs into the same problem.
Are new files downloaded during when a script is running somehow not visible to the search path? Because the folder is most definitely in my search path (seeing as it works outside of during-script downloads). It's not that it isn't getting the files fast enough either, cause they appear in my folder almost instantly, and I've tried a delay to allow for it to recognize it, but that didn't work either.
I'm not sure if it's important to note that the script calls an external function which tries to read the files from the .txt list I create in the main script.
Any ideas?
The script to download the files looks like so:
NET.addAssembly('System.Windows.Forms');
sendkey = #(strkey) System.Windows.Forms.SendKeys.SendWait(strkey);
system('start cygwinbatch.bat')
pause(.1)
sendkey(callStr1)
sendkey('{ENTER}')
pause(.1)
sendkey(callStr2)
sendkey('{ENTER}')
pause(.1)
sendkey('exit')
pause(2)
sendkey('{ENTER}')
But that is not the main reason I am asking: I am confident that the downloads are occurring when the script calls them, because I see them appearing in my folder as it called. I am more confused as to why MATLAB doesn't seem to know they are there while the script is running, and I have to stop it and run it again for it to recognize the ones I've downloaded already.
Thank you,
Aaron
The answer here is probably to run the 'rehash' function. Matlab does not look for new files while executing an operation, and in some environments misses new files even during interactive activity.
Running the rehash function forces Matlab to search through its full path and determine if there are any new files.
I've never tried to run rehash in the middle of an operation though. ...
My guess is that the MATLAB interpreter is trying to look ahead and is throwing errors based on a snapshot of what the filesystem looked like before the files were downloaded. Do you get different behavior if you run it one line at a time using F9? If that's the case then you may be able to prevent the interpreter from looking ahead by using eval().