필터 지우기
필터 지우기

How to change folders in MATLAB App Designer ?

조회 수: 26 (최근 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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by