How to read specific lines from a text file and store them in a matrix?
1 次查看(过去 30 天)
显示 更早的评论
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!
采纳的回答
Walter Roberson
2021-4-20
t = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/589822/20200926T000414.txt');
t(1:5,:)
18 个评论
Remember Samu
2021-4-20
thank you so much it worked, now I need to create a column vector of Var4 column, how would I do that?
Remember Samu
2021-4-20
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
2021-4-20
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
2021-4-20
Thanks, please try access t(1:900,:) and see if it’s giving all the values. It is refusing
Walter Roberson
2021-4-20
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
2021-4-20
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
2021-5-10
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
2021-5-10
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
2021-5-11
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});
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)