Read real numbers with textread

I have to read specific lines in a .txt file and thus I am using the function textread to do so. The problem is that the lines are composed of real numbers and then textread saves it as '0.957' and not as 0.957 (no commas). Because of that, I cannot save these data into a vector. I tried to remove the commas with regexprep but it didn't work. How can I do that? This is my code (that returns an error):
clc;
clear variables;
close all;
file=fopen('takt_times.txt','r');
nlines=0;
while (fgets(file) ~= -1),
nlines = nlines+1;
end
fclose(file);
C = textread('takt_times.txt', '%f','delimiter', '\n');
read1=2;
rowindex=1;
a=zeros(40,1);
i=1;
while rowindex < nlines
cc=C{read1};
a(i)=regexprep(cc,"'", "");
read1=read1+5;
rowindex=rowindex+5;
i=i+1;
end
a

8 件のコメント

Stephen23
Stephen23 2018 年 5 月 4 日
編集済み: Stephen23 2018 年 5 月 4 日
@NF: please upload your data file by clicking the paperclip button.
If you want to import the data as numeric data, then why are you using the string format %q ?
NF
NF 2018 年 5 月 4 日
I had just finished to try the different formats and forgot to put a reasonable one in.
NF
NF 2018 年 5 月 4 日
Here you are the file.
Stephen23
Stephen23 2018 年 5 月 4 日
@NF: please upload your data file by clicking the paperclip button.
NF
NF 2018 年 5 月 4 日
Sorry, I misunderstood.
Stephen23
Stephen23 2018 年 5 月 4 日
編集済み: Stephen23 2018 年 5 月 4 日
"I have to read specific lines in a .txt file "
What are the specific lines that you want to read? How are they identified? E.g. what features do they have?
Guillaume
Guillaume 2018 年 5 月 4 日
Note: ' is an apostrophe (or a single quote), a comma is ,.
The code in the question differs from the code in your m file. Which do you use? %f will not output '0.957', %s will.
Reading the file twice (once to count the number of lines, once to parse it) is a waste of time. What exactly do you want to extract from the file?
NF
NF 2018 年 5 月 4 日
I need to extract and save into a vector some of the numbers in the .txt file, that are distanciated always by the same measure, for example each 6 lines there is the one that I have to save, starting from the third line.
The lines simply are real numbers.
Example:
Line 1
Line 2
Line 3 % save
Line 4
Line 5
Line 6
Line 7
Line 8 % save
Line 9
Line 10
Line 11
Line 12
Line 13 % save
... % and so on
P.S. In the .txt file there are also lines with more thant one value, those lines are not interested in this saving.
@guillaume, yes, there are some differencies in the two because meanwhile I was trying some solutions, however the main code is quite the same. I know the %f and %s thing, I just copied the last trial I had.

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

 採用された回答

Guillaume
Guillaume 2018 年 5 月 4 日

0 投票

Here's how I'd do it:
wholecontent = fileread('takt_times.txt');
lines = strsplit(wholecontent, '\n')';
linestokeep = lines(3:5:end);
numbers = str2double(linestokeep);

1 件のコメント

NF
NF 2018 年 5 月 4 日
Thanks, that's perfect

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日

0 投票

Try using textscan() and specify format using '%f'. It will read the data as floating point numbers.
C = textscan('takt_times.txt', '%f','delimiter', '\n');

3 件のコメント

NF
NF 2018 年 5 月 4 日
Do you mean to leave the code as before except for using textscan instead of textread?
Guillaume
Guillaume 2018 年 5 月 4 日
textread was deprecated many versions ago, but the two are more or less equivalent, the major difference being that textscan returns a cell array wheras textread returns several output.
You should be using textscan instead of textread but just changing the function will have no effect on how the file is parsed.
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
@NF, the textscan will load entire and separate into different parts using delimiter. So, in this case, you will get a cell array and each cell contains one line from txt file. It is same as what you will get from textread.

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

カテゴリ

ヘルプ センター および File ExchangeLarge Files and Big Data についてさらに検索

タグ

質問済み:

NF
2018 年 5 月 4 日

コメント済み:

NF
2018 年 5 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by