Matlab parfor in different script file "exist" error - matlab

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.

Related

How do i get all lines of my MatLab (.m) files to run?

I have a .m file, that when i run it manually (meaning already in MatLab, and then type the three lines) it runs just like it should.
What im trying to do is put those three lines into a m file and have it run instead of me having to type the lines once im in MatLab
This is the m file im trying to run its called "ABOVE2019_TF01_MatLabCommands_Test.m"
These are the three lines in it:
in_dir_list = {'/j078_8/58667_TF01_G11','/j078_8/58667_TF01_G09',};
out_dir_list = {'/j078_8/58667_TF01_G11','/j078_8/58667_TF01_G09',};
resid_process_GPS(in_dir_list,out_dir_list);
(again running those three lines within MatLab works exactly like I would expect)
So i try to run the ABOVE2019_TF01_MatLabCommands_Test.m file like this
/Applications/MATLAB_R2017a.app/bin/matlab -r "cd /volumes/promiseraid9/workspace/colleen/NewResiduals/j078_8; try, run('ABOVE2019_TF01_MatLabCommands_Test.m'); end; quit"
It doesn't error or anything it just exits out of MatLab instead of running the third line
If instead i changed the third line of the m file to just print out what in_dir_list it. The above command will print that out no problem. But the problem comes with the third line. For whatever reason the above code will not run the third line. What am i doing wrong?
You are using a try statement without catching or handling any exceptions that occur. Due to this, any errors which occur inside your script ABOVE2019_TF01_MatLabCommands_Test.m would not be returned to the command line.
You can verify this by running the following code from the command line:
try; asdfasdfalwelknwerewr_THIS_LINE_SHOULD_ERROR; end;
You should use a catch statement to handle any exceptions returned.
If you are running this from outside of the MATLAB desktop environment (which maintains a path to which to search for functions), are you sure that your functions are located within the search path? As in, is resid_process_GPS located within the folder named /j078_8?

Why do I need to run a file several times in MATLAB before it stops throwing an error?

I have been trying to debug this weird bug where I run a script in matlab and it says that it cannot find some function that I have clearly defined in a folder and also obviously imported to the running script. However, after running the script a few times it suddenly accepts it knows where the location of my function is and runs. I find this really strange because that shouldn't be happening because imports are deterministic functions, so I have no idea why running it multiple times should make a difference.
To reproduce my bug you can find my code in my project's github page. Go/cd to the folder:
research/HBF_mat_lib/HBF1_multivariant_regression/om_simulations/h_add_cv
and
run:
test_debug_script
in MATLAB.
It show throw an error:
Undefined function 'learn_HBF1_SGD' for input
arguments of type 'HBF1_parameters'.
Error in get_best_trained_hbf1_model (line 37)
[ mdl_params, errors_train, errors_test ] =
train_func( X_train, y_train, mdl_params,
iterations,visualize, X_test,y_test,
eta_c,eta_t, sgd_errors);
Error in test_debug_script (line 11)
get_best_trained_hbf1_model(slurm_job_id,
task_id)
Error in run (line 96)
evalin('caller', [script ';']);
but if you try running it enough times it runs as it should.
Things I've tried to clear this bug is clear my matlab environment, restore my path to normal, at this point I have no idea what I can do because I have also printed out what path is before executing stuff and the folder seems to be in my path variable. At this point I have no idea what I can do and why after running a few times it runs, it should ALWAYS fail but it does not...
I'd venture it's because you're creating a reference to learn_HBF1_SGD in simulation_config.m before you put the function on the Matlab path via load_paths.m, which you've made dependent on the function handle's content. Given the current structure, I'd switch to declaring train_func_name explicitly, and then use str2func to create the handle after the paths have been loaded.
Subsequent runs of the file work because the execution did not stop until after the calls to addpath have been made. And since addpath "adds the specified folders to the top of the search path for the current MATLABĀ® session", the function was on the Matlab path for subsequent runs.

fopen error - works for a while but then gives an error

I have a script that is running a series of for loops, and within these for loops a file is created that is then run using an external program using the script command. In summary it looks like this:
for i=1:n1
for j=1:n2
for k=1:n3
fid=fopen('file.txt','w');
fprintf(fid,'Some commands to pass to external program depending on i j k');
fclose(fid);
system('program file.txt');
end
end
end
The script has in total about 500k cases (n1xn2xn3), and runs fine for a small scenario (about 100 runs), but for the entire script it runs for a while and then returns an error for no apparent reason, giving this error:
fopen invalid file identifier object
There is no obvious reason for this, and Im wondering if anyone could point out what is wrong?
Just a guess: an instance of your external program is reading file.txt and at the same time the next iteration of your nested loop wants to open file.txt for writing. The more instances of your external program are running at the same time, the slower your machine, the more likely becomes this scenario. (called a 'race condition')
Possible solution for this: use a separate text file per case with a unique file name
You should also consider using other ways to call your external function because file handling for 500k cases should be very slow.
Hope that helps,
Eli

Matlab - Can't call function because it says I'm trying to execute a script

In Matlab I defined a function called iReadImage it looks like:
function [outimag] = iReadImage(imaurl)
{code}
I used it for hours and everything seemed to work fine but then I changed one line and all of a sudden it didn't work anymore, even after I deleted that line nothing worked. It always tells me:
Attempt to execute SCRIPT iReadImage as a function:
/home/.../iReadImage.m
When I look at the file it says that it is 0kB....No idea why, I tried kind of everything, copied the function to a new function, rebooted my computer even tried it on other PCs. Two or three times it seemed to work again but never for long until I got the same error message.
Matlab is very particular about how its functions can be constructed. The file functionname.m should start with the first line function [output] = functionname(input). Otherwise, it will assume that it is dealing with a script and not a function. Additionally, if your file is a function, you can declare within it, like:
function y = f(x)
y = g(x) + 2;
function z = g(x)
z = x.^2;
end
end
However, if your file is a script, Matlab does not allow such function declarations. One way to test this would be to trivially turn your existing script into a function (by wrapping it with a function with null input and output), and see if he same error occurs.
The problem is probably that you've changed your working directory (using e.g. cd). You can only run functions that are in the current working directory, or in directories listed in path.
To confirm, type which iReadImage.
My guess is that you have multiple iReadImage files in the directories where matlab searches for scripts and functions. If so it's likley that Matlab have found the wrong one (perhaps one with an error in it?) and tries to execute it.
Make sure you only have one copy of the file (check which directories Matlab searches in with path).
To find out from which directory Matlab will execute your function write which <filename>, that is, in your case which iReadImage and make sure the correct file is used.
You can also use which iReadImage -all to find all iReadImage files.

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