How to read a text file line by line and store its element into two arrays.

21 ビュー (過去 30 日間)
shubham shubham
shubham shubham 2020 年 11 月 2 日
コメント済み: Sudhakar Shinde 2020 年 11 月 3 日
So I have a text file with data like this, and i want to read it in such a way that first element goes into first array and second into second array.
0.000708 0.000595
0.000697 0.000562
0.000640 0.000551
0.000618 0.000551
0.000595 0.000528
so output should be something like
0.000595
0.000562
0.000551
0.000551
0.000528
all the above elements in one array and all the elements below in another array or vector
0.000708
0.000697
0.000640
0.000618
0.000595
  1 件のコメント
dpb
dpb 2020 年 11 月 2 日
Why? Why duplicate data but not just use the two column indices?
But, if you insist,
data=readmatrix('yourfile.dat');
A=data(:,1);
B=data(:,2);
clear data
Less hassle in creating the vectors would be to use
tAB=readtable('yourfile.dat');
tAB.Properties.VariableNames={'A','B'};

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

回答 (1 件)

Sudhakar Shinde
Sudhakar Shinde 2020 年 11 月 3 日
編集済み: Sudhakar Shinde 2020 年 11 月 3 日
Check:
Table = readtable('InputFile.txt'); %Read text file in table format
FirstColumn = Table(:,1); % First column data
SecondColumn = Table(:,2); %Second column data

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by