You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to read specific lines from a text file and store them in a matrix?
8 views (last 30 days)
Show older comments
Hello everyone,
I want to create a matrix from the attached text file that only includes the lines after the one that reads "#begindata". Basically, I want a matrix that includes the headers of the columns and all the numeric data from the file, but none of the information preceding it.
Also, would it be possible to use the same code when reading information from a file with a different number or rows and columns?
Thank you!
Accepted Answer
Walter Roberson
on 20 Apr 2021
t = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/589822/20200926T000414.txt');
t(1:5,:)
18 Comments
Remember Samu
on 20 Apr 2021
thank you so much it worked, now I need to create a column vector of Var4 column, how would I do that?
Remember Samu
on 20 Apr 2021
Thank you so much, the code seem to stop if there are any missing values, it will not access the whole column, it stops with an NaN. do you know how i can bypass this or if I can input 0 whenever it sees no value?
Walter Roberson
on 20 Apr 2021
Could you give some example lines with missing values?
I scrolled through the data you posted but did not notice any missing entries.
Remember Samu
on 20 Apr 2021
Thanks, please try access t(1:900,:) and see if it’s giving all the values. It is refusing
Walter Roberson
on 20 Apr 2021
t = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/589822/20200926T000414.txt');
height(t)
ans = 60508
t(900:905,:)
ans = 6×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7
__________ ____________ ____ ____ ____ ____ ____
19/09/2020 02:29:50.228 1.9 -0.5 2.5 11.8 16
19/09/2020 02:30:00.214 1.9 -0.5 2.5 11.8 16
19/09/2020 02:30:10.217 1.8 -0.5 2.4 11.8 16
19/09/2020 02:30:20.221 1.8 -0.5 2.4 11.8 16
19/09/2020 02:30:30.223 1.8 -0.5 2.4 11.8 16
19/09/2020 02:30:40.213 1.8 -0.5 2.4 11.8 16
I am not observing any problem. Which MATLAB release are you using?
Walter Roberson
on 20 Apr 2021
I get a nan and NaT for the last entry (correspoding to the '#enddata' line), but otherwise it looks fine to me in R2019b on MacOS.
Remember Samu
on 20 Apr 2021
Ok thank you so much, I will try it again,, thank you so much once again.
Remember Samu
on 10 May 2021
Hi Walter, If I want to extract colums Var 1, Var 2 and Var 4 and put them into another variable named for expample refdata, how will I do it. Could you please help.
Walter Roberson
on 10 May 2021
refdata = t(:,[1 2 4]);
Note that because Var1 and Var2 are not the same datatype as Var4, you cannot put the subset into a plain matrix, only composite datatype such as a table() or cell array.
Also, you might want to consider
t.dt = t{:,1} + t{:,2};
refdata = t(:, {'dt', 4});
Remember Samu
on 11 May 2021
Hi Walter, I am facing the followign error:
Error using ValidationScriptupdated (line 11)
A table variable subscript must be a numeric array containing real
positive integers, a logical array, a character vector, a string
array, or a cell array of character vectors.
Line 10: t.dt = t{:,1} + t{:,2};
Line 11: refdata = t(:, {'dt', 4});
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)