matlabFunction randomly throws errors

5 Ansichten (letzte 30 Tage)
Jurriën
Jurriën am 16 Apr. 2018
Beantwortet: Jurriën am 24 Apr. 2018
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

Akzeptierte Antwort

Jurriën
Jurriën am 24 Apr. 2018
I contacted Mathworks support and they suggested a number of reasons that might cause this kind of error. It turns out it was caused by Dropbox. I run my script in a folder that is synced, and this syncing may prevent Matlab from writing a function and thus throw an error.
So for anyone reading this and having the same issue: don't save your scripts in a synced folder.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by