Undefined function or variable 'filename'.
10 views (last 30 days)
Show older comments
Hi,
I've written some functions to numerically integrate the equations of motion of a double pendulum. It is a first order differential equation (like ydot = f(t,y)). I am writing my own numerical integration (as part of an assignment). Now here's the thing:
The matrices used to solve for ydot (the derivative of the state) are created with the symbolic toolbox. Because I do not want to use 'subs' each time before I solve, because it is very slow, I have used 'diary' to create an m-file frmatrix.m that simply states: fr = [blabla]; So when I run it, with the variables in my workspace, it simply creates the matrix fr. I do this for an Mr and an fr because I want to calculate Mr\fr. The lines are below eachother but I get an error for fr: Undefined function or variable 'frmatrix'. It has done exactly the same for 'Mrmatrix' which doesn't fail. I have checked and all.. the name is correct and frmatrix.m exists. Now here's the thing: if I restart matlab, it works fine. If i put a keyboard in the loop so I have to press f5 on every iteration, it works. But it will work only once after restarting matlab. if i run it again, i get the error again. even if i delete frmatrix.m
I really have no clue why this is happening so I hope anyone can help me out?
Kind regards, Iris
ps. below is a minimum working example, I hope it is clear. It consists of 2 separate m-files.
function tmt(par)
syms m I l g
syms p1 p2 p1d p2d
%%%%%%%Symbolic Mr and fr are created here, I left it out for simplicity %%%%%%%
% variables m,I,l and g are given a value
m = par.m;
I = par.I;
l = par.l;
g = par.g;
% The symbols of m, I, l and g in Mr and fr are subsituted with the values
Mr = subs(Mr);
fr = subs(fr);
% the files Mrmatrix.m and frmatrix.m are created.
if exist('Mrmatrix.m', 'file')
! del Mrmatrix.m
end
diary Mrmatrix.m
disp('Mr = ['), disp(Mr), disp('];');
diary off
if exist('frmatrix.m', 'file')
! del frmatrix.m
end
diary frmatrix.m
disp('fr = ['), disp(fr), disp('];');
diary off
function statederivative = eom(t,state,par)
p1 = state(1);
p2 = state(2);
p1d = state(3);
p2d = state(4);
Mrmatrix;
frmatrix; % HERE IT FAILS!!!
qdd = double(Mr\fr);
statederivative = [state(3); state(4); qdd];
Accepted Answer
Jan
on 21 Apr 2014
Edited: Jan
on 21 Apr 2014
It is cruel to check the existence of a file by exist(name, 'file') without using an absolute path. It is cruel to delete a file by !del name, especially when no absolute path name is used. Creating a new file by disp and diary is even more cruel. Better:
folder = cd; % for example the current folder, better define it explicitly
name = 'fmatrix.m';
fid = fopen(fullfile(folder, name), 'w');
if fid == -1, error('Cannot open file for writing.'); end
fprintf(fid, 'fr = [%s];\n', fr);
fclose(fid);
This is much more direct.
If the created cannot be found, try clear fmatrix and / or rehash .
Prefer to create the m-file in a folder, which is contained in Matlab's path.
4 Comments
More Answers (1)
Walter Roberson
on 21 Apr 2014
You need to
rehash
if you create a .m on the fly and want to execute it.
See Also
Categories
Find more on Special Values in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!