How can I fopen files in different directory

조회 수: 20 (최근 30일)
Vinoth S
Vinoth S 2020년 1월 25일
댓글: Vinoth S 2020년 1월 25일
How can I fopen files that are in a different directory than current directory, fopen always returns -1.

채택된 답변

Stephen23
Stephen23 2020년 1월 25일
Do NOT follow advice of just adding more directories to the Search Path: this just pointlessly slows MATLAB down (MATLAB has to keep track of all of the files in all folders on the Search Path). In general you should NOT add data folders to the search path, because all MATLAB functions that import/export data files accept relative or absolute filenames, and this is much more efficient than bloating the Search Path. For example:
D = 'E:\mails spamnon\non spam';
S = dir(fullfile(D,'*.*'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
fid = fopen(F);
...
fclose(fid);
end

추가 답변 (1개)

stozaki
stozaki 2020년 1월 25일
Hello,
Please add the path containing the file you want to open to the MATLAB search path.
Regards,
stozaki
  댓글 수: 3
stozaki
stozaki 2020년 1월 25일
편집: stozaki 2020년 1월 25일
Please try following command.
addpath(genpath('E:\mails spamnon\non spam'))
Vinoth S
Vinoth S 2020년 1월 25일
Thanks!

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

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by