Import text with headers as column values and append to traditional data set
이전 댓글 표시
I would like to import a large number of text files from a simulation program that all have a very similar pattern into one matrix or dataset.
I am trying to pull the numbers from line three into columns, with a column for each value.
(:, Column 1)=78%
(:, Column 2)=3000
(:, Column 3)=1
(:, Column 4)=9
(:, Column 5)=5
(:, Column 6)=300
The headings of each of the values does not change across file types just the numbers do.
Then columns and rows would take the rest of the table.
mat1=(:, [date biomass yield no3() sowing_date surfacep_wt AccumRainfall])
채택된 답변
추가 답변 (1개)
Walter Roberson
2015년 5월 9일
0 개 추천
Use textscan() telling it to skip 6 lines, and use a format of '%s%g%g%g%g%g%g%s' and the CollectOutput option. You will get as output a cell array, the first entry of which is the dates in string format, the second is an N x 6 array of numbers, and the third is the string for AccumRainfall.
You can use datenum() with 'mm/dd/yyyy' format on the first cell array to get MATLAB numeric date.
The proper processing for the AcumRainfall string is not obvious. Should '?' be interpreted as 0, or do you want it to come out as NaN or as some other value? Your sample only shows '?' in that column, so I do not know if the field would say 'Y' if there was rainfall or if it would show a numeric amount. If it is a numeric amount, then you can use str2double() on the cell array of strings: that will convert all of the '?' entries into NaN values and will convert the numeric strings to numbers. The NaN can then be detected (if desired) by using isnan()
댓글 수: 2
Walter Roberson
2015년 5월 9일
You can textscan() with a string format such as '%s%s%s%s%s%s%s%s', a header skip of 1 line, and a count of 1 lines. That would leave you after line 2, so then you would do a textscan with a header skip of 4 lines and the format I gave about.
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!