필터 지우기
필터 지우기

How to import a txt file from a subfolders (not included in path)

조회 수: 5 (최근 30일)
Marius Hammer
Marius Hammer 2019년 3월 28일
편집: dpb 2019년 3월 28일
Hi, This might have been asked elsewhere, however I haven't been able to figure out a simple way of doing this.
How can I import a single .txt file located in a subdirectory of my working directory (not included in my path)?
If the file was located in my working directory I could import it as e.g.:
Data = dlmread('filname.txt',',',0,0);
However if my file is located in a subfolder, is there a way to import the file similar like this (which doesn't work)?
Data = dlmread('/subfolder/filname.txt',',',0,0);

채택된 답변

dpb
dpb 2019년 3월 28일
편집: dpb 2019년 3월 28일
You pass a qualified filename -- it can be fully-qualified or partially, as long as it is unambiguous and refers to the correct location.
In your reference above, the leading '/' means the subdirectory is directly underneath the default drive so that it is interpreted as (say) 'C:/subfolder/filename.txt' which is undoubtedly not where it actually is...
If it is directly under your current working directory, then the syntax would be
dlmread('subfolder/filename.txt')
% or
dlmread('./subfolder/filename.txt')
fullfile is useful for building qualified filenames...
wkdir='subfolder'; % get/set the working directory however wish
dlmread(fullfile(wkdir,fname)) % call function with system-consistent full filename
as it takes care of ensuring needed separator and lets you also factor the code to handle different data locations w/o changes to the code itself, just data.
  댓글 수: 1
Marius Hammer
Marius Hammer 2019년 3월 28일
So it was just to delete the slash (/), thought I had tried that... silly me. But thanks alot and for the tip about using fullfile.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by