現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to read specific lines from a text file and store them in a matrix?
10 ビュー (過去 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 年 4 月 20 日
Ok thank you so much, I will try it again,, thank you so much once again.
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 で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)