Error using cd Cannot CD to ...(Name is nonexistent or not a directory)

Hi,
I have the following code
oldp = uicontrol(fig,...
'Style', 'Push',...
'BackgroundColor', [1 1 .7],...
'units', 'normalize',...
'Position', [0 .333 1 .333],...
'String', 'Open an existing Project',...
'Callback', [...
' [projfile,projpath] = uigetfile(''*.prj'',''Select Project File '');',...
' cd(projpath);',...
' load(projfile,''cp'',''-mat'');',...
' close(gcf);',...
' clear projpath projfile newp oldp stringforhelp ui*; '...
]...
);
I am getting this error:
Error using cd
Cannot CD to C:\...(Name is nonexistent or not a directory)
Any idea? thanks

2 件のコメント

Stephen23
Stephen23 2020 年 4 月 6 日
Do NOT define the callback as a character vector. The MATLAB documentation
states "Defining a callback as a character vector is not recommended". Specifying the callback with a function handle is recommended, and will make debugging your code much easier.
Do NOT use cd just to read/write data files. All MATLAB functions that read/write data files accept absolute/relative filenames: you should use fullfile to generate the absolute/relative filename and pass that to load (or whatever function you use for reading/writing data files).
Using clear is rarely required in code, and is almost always overused by beginners.
Curious Mind
Curious Mind 2020 年 4 月 6 日
編集済み: Curious Mind 2020 年 4 月 6 日
Hi Stephen, I will admit I am a beginner in Matlab. The code worked with no issues in Matlab v2014a. I am now using 2019b and I am getting the error. Any help with how my code should look like?
Thanks!

サインインしてコメントする。

回答 (1 件)

Maadhav Akula
Maadhav Akula 2020 年 4 月 13 日

0 投票

Hi,
You can write the following lines in your callback function:
[projfile,projpath] = uigetfile('*.prj','Select Project File ');
abs_filepath = fullfile(projpath,projfile);
load(abs_filepath,'cp','-mat');
Hope this helps!

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2020 年 4 月 6 日

回答済み:

2020 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by