Load in multiple text files and store data in a matrix

2 ビュー (過去 30 日間)
Benjamin Cowen
Benjamin Cowen 2018 年 8 月 16 日
編集済み: Walter Roberson 2018 年 12 月 5 日
I have 28 text files, of this pattern:
out.200.data
out.225.data
up to
out.900.data
How can I populate the 3rd row into a matrix? Note that there are 3 columns in the 3rd row. I also have a path to my files, but for purposes here, let's just say the path is just C:/Path
Is there a way to create a single matrix with these values?
Row 1 of the matrix is the 3rd row of out.200.data
Row 2 of the matrix is the 3rd row of out.225.data
etc?
  2 件のコメント
TAB
TAB 2018 年 8 月 17 日
What is format of your data in text file?
Benjamin Cowen
Benjamin Cowen 2018 年 8 月 17 日
編集済み: Walter Roberson 2018 年 8 月 17 日
2 header rows
3rd row is 3 numbers, separated by spaces

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 8 月 17 日
for information on looping over files.
For any one file, I suggest something like
filename = fullfile(projectdirectory, dinfo(K).name);
fid = fopen(filename, 'rt');
row3(K,:) = cell2mat( textscan(fid, '%f%f%f', 1, 'headerlines', 2, 'collectoutput', true) );
fclose(fid)
  4 件のコメント
Benjamin Cowen
Benjamin Cowen 2018 年 8 月 17 日
編集済み: Benjamin Cowen 2018 年 8 月 17 日
@Walter Roberson Thanks for this, it worked. If I only have 1 headerline, I want to extract the 51st row, and there are only 2 columns as opposed to 3, why does this not work:
clear
clear all
clc
projectdirectory = 'C:/Path/';
dinfo = dir( fullfile(projectdirectory, 'msd.*.data') );
N = length(dinfo);
row51 = zeros(N, 2);
for K = 1 : N
filename = fullfile(projectdirectory, dinfo(K).name);
fid = fopen(filename, 'rt');
row51(K,:) = cell2mat( textscan(fid, '%f%f', 50, 'headerlines', 1, 'collectoutput', true) );
fclose(fid);
end
Walter Roberson
Walter Roberson 2018 年 8 月 17 日
編集済み: Walter Roberson 2018 年 12 月 5 日
clear
%let us refrain from blowing up the bridge we are standing on, like Wile E. Coyote
%<<https://cdn-images-1.medium.com/max/1338/1*8oK7yw-tYvP14l_Ya3Nvcg.png>>
%clear all
clc
Row_to_extract = 51;
projectdirectory = 'C:/Path/';
dinfo = dir( fullfile(projectdirectory, 'msd.*.data') );
N = length(dinfo);
row = zeros(N, 2);
for K = 1 : N
filename = fullfile(projectdirectory, dinfo(K).name);
fid = fopen(filename, 'rt');
row(K,:) = cell2mat( textscan(fid, '%f%f', 1, 'headerlines', Row_to_extract - 1, 'collectoutput', true) );
fclose(fid);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by