How do I read all text files from a folder?

Hey all, I have four folders with text files in them in my workspace which I want my script to read into. I have tried using dir and fopen but fid = (-1). Here is the outline:
dirlist = dir('*/*.txt'); len=length(dirlist);
for K = 1: len fid = fopen(dirlist(K).name);

3 件のコメント

Star Strider
Star Strider 2018 年 1 月 28 日
If you ask fopen for the second output as well, it will tell you what the problem is:
[fid,message] = fopen(dirlist(K).name)
Norbert Kapa
Norbert Kapa 2018 年 1 月 28 日
it says 'No such file or directory'. However dirlist indeed consist of the names of all the textfiles.
Star Strider
Star Strider 2018 年 1 月 28 日
I notice you use a forward slant (/), so you must be using Linux. On Windows systems, the first two outputs from a dir call are:
.
..
and will throw that error. Check to see what ‘dirlist’ actually returns to be sure all the data returned are valid file names. It would likely be best to display the results of:
FileName = dirlist(K).name
for diagnostic purposes until you discover the problem.
(I am not listing this as an Answer because I do not have Linux to test this myself.)

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

回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 28 日

0 投票

It's one of the most F of F.A.Q.'s. Go here for the answer http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Basically, you forgot to use fullfile() to prepend the folder.

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

タグ

質問済み:

2018 年 1 月 28 日

コメント済み:

2018 年 1 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by