이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)