Undefined function or variable
Show older comments
When I tried to run the file "project.m" in the folder "C:\Users\MyName\Documents\MATLAB", it always showed the error "Undefined function or variable 'project'" even though I already set PATH for MATLAB to point to that MATLAB folder. I have a student license lasting till Oct 31, 2018
19 Comments
Guillaume
on 4 Dec 2017
"even though I already set PATH for MATLAB"
Clearly, that did not work. How do you set the matlab path?
Walter Roberson
on 4 Dec 2017
Please show the output of:
folder = 'C:\Users\Thao Ha\Documents\MATLAB';
if ~exist(folder, 'dir')
fprintf('folder "%s" does not exist?\n', folder);
else
dinfo = dir( fullfile(folder, 'project*.*') );
if isempty(dinfo)
fprintf('folder "%s" does not have any project*.* files\n', folder);
else
fprintf('project*.* files in folder "%s" are:\n', folder);
celldisp({dinfo.name});
fprintf('\n');
end
dinfo = dir( fullfile(folder, '*.m') );
if isempty(dinfo)
fprintf('folder "%s" does not have any *.m files\n', folder);
else
fprintf('*.m files in folder "%s" are:\n', folder);
celldisp({dinfo.name});
fprintf('\n');
end
end
Walter Roberson
on 4 Dec 2017
You accidentally copied my code as 'dir ' instead of 'dir'. Also you copied
dinfo = dir( fullfile(folder, '*.m') );
as
dinfo = dir( fullfile(folder, '*.m ') );
I have attached the code as a .m file that you can download and run.
Thao Ha
on 4 Dec 2017
Walter Roberson
on 4 Dec 2017
Download it into a directory, cd to the directory and try
run('testpath.m')
Walter Roberson
on 4 Dec 2017
Edited: Walter Roberson
on 4 Dec 2017
Please show the output of executing
which path
which matlabpath
matlabpath
inside MATLAB
Thao Ha
on 5 Dec 2017
Edited: Walter Roberson
on 5 Dec 2017
Walter Roberson
on 5 Dec 2017
Try
cd('C:\Users\Thao Ha\Documents\MATLAB')
ls('p*.m') + 0
and show us the output
Thao Ha
on 5 Dec 2017
Edited: Walter Roberson
on 5 Dec 2017
Walter Roberson
on 5 Dec 2017
You have a space at the end of 'MATLAB ' in the test that worked. If that space is part of the directory name then you are going to confuse people including yourself.
cd('C:\Users\Thao Ha\Documents\MATLAB ')
is not the same as
cd('C:\Users\Thao Ha\Documents\MATLAB')
Thao Ha
on 5 Dec 2017
Walter Roberson
on 5 Dec 2017
What happens if you
cd('C:\Users\Thao Ha\Documents\MATLAB')
run('project.m')
Thao Ha
on 5 Dec 2017
Edited: Walter Roberson
on 5 Dec 2017
Walter Roberson
on 5 Dec 2017
Please go back to the testpath.m that I posted in https://www.mathworks.com/matlabcentral/answers/370999-undefined-function-or-variable#comment_512503 and download it to a directory, and cd to that directory, and
run('testpath.m')
KL
on 5 Dec 2017
What happens if you close matlab and double-click open your m-file from your file explorer. That should open the file with its corresponding folder as working directory.
Thao Ha
on 5 Dec 2017
Answers (2)
KL
on 4 Dec 2017
You still haven't answered how you set the path. Use addpath,
addpath('C:\Users\MyName\Documents\MATLAB')
1 Comment
Walter Roberson
on 5 Dec 2017
0 votes
You need to contact Mathworks for installation support.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


