Built in function cd not being found when running a custom function

11 views (last 30 days)
Hello, I am having an issue where cd is not being found when I run a custom script. The relevant script is as follows, [tmp] being a placeholder that removes identifying information. cvfn is the name of a file that is located in basepath. Basepath is the folder that should be the working directory at the start of the function.
function horm_symptom(cvfn)
close all;
basepath={'C:\\Users\\[tmp]_WorkingFolder';
'C:\\Users\\[tmp]_WorkingFolder'};
if exist(basepath{1},'dir'), basepath=basepath{1};
elseif exist(basepath{2},'dir'), basepath=basepath{2};
else, error('basepath does not exist');
end
addpath(genpath(basepath));
savepath=fullfile(basepath,'SingleSubjectData');
if ~exist(savepath,'dir'), mkdir(savepath); end
cd('SingleSubjectData');
When i run the script I get the following error at the line cd('SingleSubjectData')
Unrecognized function or variable 'cd'.
However, if I set a breakpoint at 'cd('SingleSubjectData');' and type the following, I get:
exist('cd','builtin')
ans =
5
which('cd')
ans =
built-in (C:\Program Files\MATLAB\R2023b\toolbox\matlab\general\cd)
I've also successfully used addpath to the above directory with no errors, but even still I get the unrecognized function error.
addpath('C:\Program Files\MATLAB\R2023b\toolbox\matlab\general')
I've tried different formats for cd, including adding and removing parentheses, semi-colons, and tildes and nothing has worked.
If I set a break point and type the following directly into the command window it works and changes directory,
cd 'SingleSubjectData'
However, if I try to run the code with the above formatting within the script I get the following error:
horm_symptom(cvfn)
Error: File: horm_symptom.m Line: 21 Column: 1
Using identifier 'cd' as both a variable and a command is not supported. For more information, see "How MATLAB
Recognizes Command Syntax".
The above error occurs when I try any of the following formats:
cd SingleSubjectData;
cd SingleSubjectData
cd 'SingleSubjectData'
cd 'SingleSubjectData';
cd ~/SingleSubjectData
I have checked that a variable named "cd" is not getting created, confirmed that the sub folder SingleSubjectData is getting created and does exist and reviewed the How Matlab recognizes command syntax page linked in the error. I am at a loss of how to fix this or whats wrong. Lastly, I know I'm talking about SingleSubjectData, but I have also tried cding into savepath, and have had all the same errors described above. Please help!

Accepted Answer

Meggo
Meggo on 1 Oct 2024
Edited: Meggo on 1 Oct 2024
Took some time but I figured out the issue:
A line was partially deleted later in the function which was creating a cd variable! the line wasn't run at the time of cd being used as a command so it wasn't showing up in the workspace, but I guess the existence of the line later in the function prevented matlab from being able to run the builtin function.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!