read specific column of text file using Matlab

조회 수: 3(최근 30일)
Keqiao Li
Keqiao Li 2015년 5월 14일
답변: Walter Roberson 2015년 5월 14일
My text file looks like the following text:
69.600000, 56.500000, 19991101, 0, 16.000000, 1, 5,3GGXC6,UCTP, 792,, 69.4139, 56.5321, 20725.632, 26267346, 7.33496,/Net/tholia/qscat/data/nc_test_daily/1999/qs_l2b_1999305.nc, 935, 71
It is the first line of my text file. The rest of lines of the text file has same format I'm trying to read specific column of this text file by using Matlab, but it contains number and string.So How to get certain column of this text file? Need some help to solve this problem. Thanks

답변(2개)

Star Strider
Star Strider 2015년 5월 14일
If all the rows of your file are of the same format, the textscan function could probably read it. You would use the ‘format specification’ to choose the column to read.

Walter Roberson
Walter Roberson 2015년 5월 14일
colpos = 42; %starting column example
colwid = 7; %column width example
regpat = sprintf('^(?:.{%d}).{%d}', colpos-1, colwid);
filestr = fileread('YourTextFile.txt'); %read file into string
colcontents = regexp(filestr, regpat, 'match', 'lineanchors');
colcontents will now be a cell array of strings, one per entry per line, containing whatever characters were in those columns. If appropriate you can process the cell array further such as by using str2double(colcontents) if you want to interpret them as numbers.

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by