change workdir and open file

조회 수: 3 (최근 30일)
Sven Schoeberichts
Sven Schoeberichts 2011년 12월 1일
[ afile workdir ] = uigetfile( '*.dat','Select DAT-file' );
shortfilename = strrep( afile, '.DAT', '' );
cd( workdir );
fid = fopen( afile );
Whenever I run this code the first time, and select a file that is in another directory (not my workdir or a path that is added) I get an error about Matlab not able to open the file. At that point, Matlab's Current Folder window switches to the directory where the DAT file is. When I run it again, it works like it is supposed to.
How can I fix this issue? So what do I need to change that whenever I select a file that is not in a folder that is my workdir or added path, that it switches the workdir to that directory and opens the file...
  댓글 수: 1
Hin Kwan Wong
Hin Kwan Wong 2011년 12월 1일
try
fid = fopen([workdir,afile]);
instead
Also, I am worried about the fact that you removed the extension with strrep when asking matlab to do fopen. That can cause problems.

댓글을 달려면 로그인하십시오.

채택된 답변

Sven Schoeberichts
Sven Schoeberichts 2011년 12월 1일
Oops, my fault. The line you are referring to should not be there, because it's not part of the problem. That line is used later in the program.
But your comment got me on the right track. Eventually "addpath()" did the trick.
Thanks!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by