Question about getting text from a file
2 views (last 30 days)
Show older comments
Hello,
I am creating a program that is gathering information from a file listing data values, an alphanumeric call sign, and date. I can access the data, and am trying to get the call signs in the third column. I would like to use these call signs to group the data values and dates for a plot. The .dat file is similar to this:
-55.00** 6.30** 2086** 2011-11-11 01**<br>
21.70** -17.70** 2AJI3** 2011-11-07 18**<br>
20.20** -17.80** 2AJI3** 2011-11-07 22**<br>
17.50** -17.80** 2AJI3** 2011-11-08 06**
Where * represents a spacing. (not all spacings are the same) So I can get the information from a textread command that puts it all in a 1 by X matrix where x is the total number of lines.
load 'sample.dat';
la = sample(:,1); %gets column 1 from file%
lo = sample(:,2); %gets column 2 from file%
sh = sample(:,3); %gets column 3 from file% (needs to be a string)
yr = sample(:,4); %gets column 4 from file (needs to reflect actual date, or just be a string)%
pos = [la,lo];
data = textread('sample.dat','%s','delimiter','\n','whitespace','');
[col1, col2, col3] = textread('sample.dat','%n%n%n%*[\n]','delimiter',' ');
Now the textread at the bottom will get a col3 that is the correct length, but will truncate everything after the 2 of the third column. So, 2AJI3 will only return as 2 in the matrix I am creating. I would like to try and use textscan, but I have an older version of matlab, and do not know if I can update.
I know this is long, and I apologize, I am just trying to get the most information down that might help. Please be patient with me as I have not done this sort of data sorting before.
Thanks in advance.
0 Comments
Accepted Answer
Fangjun Jiang
on 7 Dec 2011
I copied your text file data and removed all the "**" and "<br>" and then run the following command. It got all the data in place. I think you can start from there.
>> [a,b,c,d,e]=textread('test.txt','%f %f %s %s %f')
a =
-55.0000
21.7000
20.2000
17.5000
b =
6.3000
-17.7000
-17.8000
-17.8000
c =
'2086'
'2AJI3'
'2AJI3'
'2AJI3'
d =
'2011-11-11'
'2011-11-07'
'2011-11-07'
'2011-11-08'
e =
1
18
22
6
More Answers (0)
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!