Import text with headers as column values and append to traditional data set
Mostrar comentarios más antiguos
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])
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 9 de Mayo de 2015
0 votos
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 comentarios
Walter Roberson
el 9 de Mayo de 2015
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.
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!