Borrar filtros
Borrar filtros

Using function files formed and saved during execution

1 visualización (últimos 30 días)
Damjan Lasic
Damjan Lasic el 16 de Dic. de 2016
Hi!
I have a somewhat unusual problem. In my code (MATLAB R2016a), i have to provide an analytical Jacobian to a solver, which i generate automatically in my code, export as a .m file, and later pass it to the solver. The problem is that MATLAB seems to not completely finish the export before continuing to next statements, causing it to sometimes pass an older iteration of the function to the solver. This can obviously cause unintended results.
I am posting below a few lines of code to demonstrate this behaviour.
txt_1 = 'function a=fun_1(b,c) \n a=b*c; \n end';
fileID = fopen('fun_1.m','w');
fprintf(fileID,txt_1);
fun_1(2,4)
pause(2)
txt_2 = 'function a=fun_1(b,c,d) \n a=b*c*d; \n end';
fileID = fopen('fun_1.m','w');
fprintf(fileID,txt_2);
fun_1(2,3,4)
ans =
8
ans =
24
In this case, the code works without a problem. The expression is defined as text, exported as a.m file, and ran. When we remove the pause line (or lower the pause value to say 1e-5), MATLAB throws an error: Not enough input arguments. This means that MATLAB used the previously saved function with two inputs for the last call.
txt_1 = 'function a=fun_1(b,c) \n a=b*c; \n end';
fileID = fopen('fun_1.m','w');
fprintf(fileID,txt_1);
fun_1(2,4)
pause(1e-5)
txt_2 = 'function a=fun_1(b,c,d) \n a=b*c*d; \n end';
fileID = fopen('fun_1.m','w');
fprintf(fileID,txt_2);
fun_1(2,3,4)
ans =
8
Error using fun_1
Too many input
arguments.
Now i would like to know the background this behaviour is related to so i can provide relability to my code. Does MATLAB save the function in the background while continuing execution? Does MATLAB save it completely but it's still not indexed properly by Windows? Maybe MATLAB just sends a command to windows to write it to disk and is then done with it?
I'm asking this in order to at least determine if and how the pause length should be dependant on file size etc.

Respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by