Undefined function or variable 'filename'.

1 view (last 30 days)
Iris
Iris on 21 Apr 2014
Edited: Iris on 26 Jun 2014
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];
  2 Comments
Star Strider
Star Strider on 21 Apr 2014
Is there some reason you must use the Symbolic Math Toolbox here rather than using ode45?
Iris
Iris on 26 Jun 2014
The symbolic toolbox is used here to derive the equations of motion which is not the same as numerically integrating them.

Sign in to comment.

Accepted Answer

Jan
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
Jan
Jan on 22 Apr 2014
Please post your code. Otherwise it is much harder to suggest a modification. You can convert a symbolic expression to a string by the char command.
Iris
Iris on 26 Jun 2014
Edited: Iris on 26 Jun 2014
It has been a while but I now know that matlabFunction solves my problem. Thanx for your answer though, it certainly helped me understand things better. I'll accept the answer. It may be a solution for others and it includes my solution in this comment.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 21 Apr 2014
You need to
rehash
if you create a .m on the fly and want to execute it.
  2 Comments
Walter Roberson
Walter Roberson on 22 Apr 2014
Faster than a general rehash would be to
clear frmatrix
Iris
Iris on 26 Jun 2014
Thanx for your reply. It has been a while but unfortunately this did not do the trick although I do not remember why exactly

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!