Read .txt and Write in matab

1 回表示 (過去 30 日間)
vaya putra
vaya putra 2019 年 11 月 11 日
コメント済み: Rik 2019 年 11 月 13 日
Hi
i have 30000 thousand line of input file in text file, but i only need line 2618-3400.
imposible if i have to write manual to the end
how to write value C that consist of data from 2618-3400
A=readfile('data.txt')';
%line 2618 to 3484
C =[A{2618,1} A{2619,1} A{2620,1} A{2621,1} A{2622,1} A{2623,1} A{2624,1}]
format long g
pf = sscanf(C,'%f')
  7 件のコメント
vaya putra
vaya putra 2019 年 11 月 11 日
more complexs and its become char?
Adam Danz
Adam Danz 2019 年 11 月 11 日
編集済み: Adam Danz 2019 年 11 月 12 日
Reading in the entire file is MUCH less complex than the method you're currently using. The data are read in as char just like the method you're already using. That's why you're applying sscanf.

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

採用された回答

Rik
Rik 2019 年 11 月 12 日
I suspect this is what you need:
C=readfile('output.txt');%read the file to a cell array
Ca=C(2618:3484);%select the lines
%process every line to doubles
Cb=cellfun(@(x) sscanf(x,'%f'),Ca,'UniformOutput',false);
%convert to matrix
out=cell2mat(Cb)';
  2 件のコメント
vaya putra
vaya putra 2019 年 11 月 12 日
perfect
Rik
Rik 2019 年 11 月 12 日
If this solved your question, feel free to mark it as accepted answer. If not, feel free to comment with your remaining issues.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2019 年 11 月 11 日
Read in the whole file at once. In this example I'm using fileread() because I'm unfamiliar with the FEX submission readfile() mentioned in your question.
C = fileread('myTextFile.txt');
Split the char array by lines into a cell array
Ca = strsplit(C,newline);
Extract text lines n to m
Ca(n:m)
If you'd rather have a char array as output
char(Ca(n:m))
  3 件のコメント
Adam Danz
Adam Danz 2019 年 11 月 13 日
I just had the chance to check out that function. I see why it would be useful!
Rik
Rik 2019 年 11 月 13 日
You can probably imagine the frustration that led to me writing this function. I admit it is a strange use case to want a text file reader that handles UTF8 and ANSI for both Matlab and Octave. I even managed to find a bug in the Octave implemention of textscan.
If you have any suggestions for how to make the function more robust, please feel free to comment on the FEX (or send me an email, you can get the address from the function doc).

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

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by