Warning: Unable to load Toolbox Path Cache in a for loop - matlab

I have a code that is like this
echo "try;
start=tic;
sv=fopen('$SIZE_VECTOR_1','r');
vector=fscanf(sv, '%d');
fclose(sv);
tama=fopen('$TAMANO_EVAL','r');
taman=fscanf(tama, '%d');
fclose(tama);
fid=fopen('$LISTA_WORDS_EVAL','r');
lista=fscanf(fid,'%f',[taman,vector]);
fclose(fid);
v_writehtk('${OUT_DIR_FEA_EVAL}/${name}.fea',lista,0.01,6);
toc(start)
catch
disp('THROWN MATLAB ERROR:');
e=lasterror;
disp(e);
e.message
e.stack.file
e.stack.name
e.stack.line
end
" > ${OUT_DIR_VEC_EVAL}/${M_FILE}.m;
I have to do this matlab code multiple times in a loop with different values, It is a "for loop" that takes different files with different names, so the for loop is in my shell script code and at each loop this matlab code is created in a txt file and then executed by matlab, I am not doing it in parallel. v_writehtk is a matlab code that transform the files taked by the loop and transforms it to another format.
sometimes in the loop I receive the Warning: Unable to load Toolbox Path Cache , so I want to know whether or not this affect to my results and how can I solve this?, I tried things like "exit" at the end of the code or "rehash toolboxcache" at the beginning and end of the code but I keep receiving this warning sometimes. Please someone help me on this.

Related

Matlab: track all file handles that were opened/touched by script

I want to find all dependencies of my script (and/or all subfunctions/subscripts), but not only functions that are called but also external files that are loaded or touched (fopen). matlab.codetools.requiredFilesAndProducts (documentation here) doesn't capture the touched filenames, my next idea, shadow the fopen function using something like below (to copy the filename to a log file), resulted in an unexpected recursive loop.
failed because of (unexpected) recursive loop, where somehow the 'builtin' fopen is not called, rather this function itself:
function varargout=fopen(varargin)
%write opened filename to log file somewhere
varargout=builtin('fopen',varargin{:}); %attempt to call built-in fopen() as usual
end
I also checked Mathworks depencey reports etc, to no use. Any ideas? Thank you.

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.

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

Running external program in matlab and getting output results

I need to write a script that will run external program from matlab and to get output results from it. This program should simulate 20 variants. I made those 20 files in matlab, and I'm able to open external program, but I am not able to write a command that will simulate the files in it. And return me output results. (Input files have .idf extension and output .eso)
I tried with these and similar commands
for id=1:20;
system(sprintf('C:\...\myprogram.exe<''variant_%i.idf',id));
i=1+1;
end
or
for id=1:20;
cmd_line = '"C:\...\myprogram.exe" -f variant_%i.idf -o variant_%i.eso';
[status, result] = system(cmd_line);
i=1+1;
end
I need to do this for exam, and I had only 3 weeks of matlab and never studied programming, so I'm sorry if this is a stupid question, but I don't know where else to ask.
You may change your "myprogram.exe" in order to report the output data into a file.

Can't close text file because MATLAB uses it after failed execution

I call a function (containing fopen and fclose) from the Command Window and then after MATLAB encounters an error that I fix (the runtime of the program stops after I save my corrections), I want to delete the file it created, in order to repeat the process. However, MATLAB, somehow, still has the file open and typing in fclose(f), in the Command Window does not make MATLAB let go of the file.
function something(something)
f = fopen('something.txt', 'w');
%statments with fprintf
fclose(f);
end
You may not have access to the handle f from outside the function, in which case you can try fclose('all') from the Matlab command window.
Generally it is best practice to use a try .. catch ... statement around code that uses a file, so that you always call fclose and release the handle if an error occurs.
If you are still unable to delete the file, and assuming it is not locked by another process (for example if you are viewing it externally in Windows Notepad), it may be that you are calling library functions from Matlab and that the shared library maintains the file lock. In this case, try reloading the library using the unloadlibrary and loadlibrary commands.
Using an onCleanup object can be very useful in this case.
Here's your code rewritten to use onCleanup - the fclose will now get called when the function terminates whether normally, or because of an error.
function something(something)
f = fopen('something.txt', 'w');
closer = onCleanup( #()fclose(f) );
% statements with fprintf
end
The documentation also includes an example for exactly this case.