How to change folders in MATLAB App Designer ?

61 ビュー (過去 30 日間)
Heirleking
Heirleking 2022 年 9 月 7 日
コメント済み: Heirleking 2022 年 9 月 28 日
I am new to the App Designer and have code from an old Matlab app designer script (GUIDE). The old GUIDE works perfectly fine even after migrating it to App Designer. Unfortunately, I have to create it from another from zero, so I am using the code after migration. Changing folders through the code gives me an error on MATLAB.
When trying to go to the folder with the name \Concentrated-PanelZone-Pushover-Output, it goes to a folder with this destination \Concentrated-PanelZone-Pushover-Output\Concentrated-PanelZone-Pushover-Output, instead of just one. Then, It gives me the following error:
Error using cd
Unable to change current folder to....
(Name is nonexistent or not a folder).
Error in cd(cambio);

回答 (2 件)

Steven Lord
Steven Lord 2022 年 9 月 7 日
I would advise you to use fullfile to assemble your file paths rather than concatenation. I'd also be wary about assuming that pwd is the directory that you expect it to be. If you were assuming that the present working directory was the directory that contains your GUI code, that could be an invalid assumption if the directory containing your GUI was on the MATLAB search path instead.
As an example, let's say we had a directory anotherDirectory under the temporary directory whose name is returned by tempdir.
cd(tempdir)
mkdir anotherDirectory
Let's add anotherDirectory to the path.
addpath(fullfile(pwd, 'anotherDirectory'))
If we create a file in that directory:
cd anotherDirectory
!touch myfile.txt
and change directory back to tempdir, the file is still visible to MATLAB since anotherDirectory is on the path:
cd ..
which -all myfile.txt
/tmp/anotherDirectory/myfile.txt
But trying to access it using pwd won't work.
pwd
ans = '/tmp'
[fid, message] = fopen(fullfile(pwd, 'myfile.txt'))
fid = -1
message = 'No such file or directory'
You'd need to ask for it in anotherDirectory.
[fid, message] = fopen(fullfile(pwd, 'anotherDirectory', 'myfile.txt'))
fid = 3
message = 0×0 empty char array
Let's close the file.
fclose(fid);

Kevin Holly
Kevin Holly 2022 年 9 月 7 日
pwd gives the present working directory. The code saves it as aux_dir.
aux_dir = pwd
aux_dir = '/users/mss.system.rKRZek'
The line below appends aux_dir with the string attached
cambio=[aux_dir '\Concentrated-PanelZone-Pushover-Output']
cambio = '/users/mss.system.rKRZek\Concentrated-PanelZone-Pushover-Output'
I believe your aux_dir was equal to
aux_dir = '\Concentrated-PanelZone-Pushover-Output';
So, you would get this:
cambio=[aux_dir '\Concentrated-PanelZone-Pushover-Output']
cambio = '\Concentrated-PanelZone-Pushover-Output\Concentrated-PanelZone-Pushover-Output'
  3 件のコメント
Kevin Holly
Kevin Holly 2022 年 9 月 28 日
If the current path doesn't contain 'Concentrated-PanelZone-Pushover-Output',
if ~contains(pwd,'Concentrated-PanelZone-Pushover-Output')
cambio=fullfile(pwd,'Concentrated-PanelZone-Pushover-Output');
cd(cambio)
end
Is there a reason you are changing the directory? Is it just to load a particular file? If so, you can do that without chainging the directory and just placing the fullpath of file as an input to the load function.
Heirleking
Heirleking 2022 年 9 月 28 日
This code was taken from an old Matlab GUI or script where full path of file was necessary. Right now, it is not necessary, but the folder referencing it is certainly used

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by