As a replacement for global variables, I use matlabFunction to write numbers and variables to a function, which I then access as if it were a global variable. However, matlabFunction is not very reliable. At random moments it will fail and throw the following error:
Error using sym/matlabFunction>writeMATLAB (line 431)
Failed to create file TEST.m: Invalid argument.
Error in sym/matlabFunction (line 179)
g = writeMATLAB(funs,file,varnames,outputs,body, opts.Optimize, opts.Sparse);
I created this simple code to reproduce the problem:
for i=1:100
try
matlabFunction(sym(5),'File','TEST');
catch
disp(i)
end
end
For me this code generally displays 3 or 4 random numbers between 1 and 100, indicating 3 or 4 errors in running 100 iterations of the exact same code. Some days it may be less and I might require 1000 iterations to produce 4 errors.
As a workaround, I simply try to call the function again:
for i=1:10
try
matlabFunction(sym(0),'File','get_xf');
break
catch
disp('matlabFunction error')
continue
end
end
In theory, this workaround is not 100% safe. In practice, however, it suffices.
The question is, what is causing these errors and how do I prevent them? Not sure if it's relevant, but I use version R2017a under an academic license.
Kindest regards,
Jurriën