matlabFunction randomly throws errors

조회 수: 5 (최근 30일)
Jurriën
Jurriën 2018년 4월 16일
답변: Jurriën 2018년 4월 24일
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

채택된 답변

Jurriën
Jurriën 2018년 4월 24일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by