How can I import my text files into matlab?

1 回表示 (過去 30 日間)
Kristen Brusich
Kristen Brusich 2015 年 9 月 18 日
編集済み: Kirby Fears 2015 年 9 月 21 日
I have 50 .txt files that I need to define as independent variables in matlab but I keep getting an error with the code I am trying to use saying file not found. These files have 5 columns and about 2000 rows. I also only want to start reading at the second row. Is there a way to do this using a loop of some sort??

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 9 月 18 日
If you are reading the files out of a directory other than the current directory, it is important that you remember to fullfile() to get the complete name. For example,
projectdir = '/Users/Kristen/MATLAB/fish_samples';
dinfo = dir(fullfile(projectdir, '*.txt'));
for K = 1 : length(dinfo)
thisfile = fullfile( projectdir, dinfo(K).name);
...
end
  3 件のコメント
Kirby Fears
Kirby Fears 2015 年 9 月 18 日
編集済み: Kirby Fears 2015 年 9 月 21 日
Hi Kristen,
If you want the vectors collected in a cell in the first place (this will make your life easier), you should read them into a single cell from the start.
A simple modification to Walter's answer above will do this for you.
projectdir = '/Users/Kristen/MATLAB/fish_samples';
dinfo = dir(fullfile(projectdir, '*.txt'));
manifolds=cell(length(dinfo),1); % initialize cell
for K = 1 : length(dinfo)
thisfile = fullfile( projectdir, dinfo(K).name);
manifold{k} = read(thisfile);
...
end
Where the "read" function is whatever function you're using to read your files.
Walter Roberson
Walter Roberson 2015 年 9 月 18 日
You would use that only if you wanted to record the names of the files. To record the data you need to load it from the file named by thisfile
manifold{K} = load(thisfile);

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by