|
"nico cruz" <m.nicolas.cruz@mailbox.tu-berlin.de> wrote in message
news:hkrd0b$ivm$1@fred.mathworks.com...
> Hi everybody,
>
> I cant find any solution to execute an m-file script immidiately after
> creating it with fprintf.
>
> my file looks like this:
>
> S5 = regexprep(S4, '] [', '\n');
> fid = fopen('AutoJac.m','w');
> S6 = fprintf(fid,'Jac = [%s];',S5);
> fclose(fid);
> AutoJac
>
> the problem is that matlab cant find the m-file AutoJac. I have to end the
> file or put a breakpoint to be able to open it.
I strongly recommend you try another approach. This is begging for trouble
if you put this in a function file, have another file named AutoJac.m on
your path, and do not have an AutoJac.m already in the location where you're
going to create the function at runtime.
function dynamicFunctionCreation
fid = fopen('sin.m', 'wt');
fprintf(fid, 'function y = sin(x)\n');
fprintf(fid, 'y = 5*x;\n');
fclose(fid);
sin(pi)
We recommend that people avoid "poofing" variables into their workspace
because it can cause difficult-to-debug problem ... but you could run into
those same type of problems because you're "poofing" a function into
existence! You might expect the call to sin(pi) on the last line to return
5*pi, but instead it returns the sine of pi radians, as when the function is
parsed MATLAB decides that the instance of sin in dynamicFunctionCreation is
a call to the built-in function that it knows about and it can't "change its
mind" when you create the sin.m function at runtime.
BTW, you'll want to delete the sin.m created by the dynamicFunctionCreation
function once you're finished testing it out, to avoid warnings.
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|