Extracting values from an excel sheet using textscan
3 views (last 30 days)
Show older comments
Hi I want to extract the values of latitude and longitude and store it in array. Please find below my code and my excel sheet
fid = fopen('actual_tgw2639true.csv');
Data = textscan(fid, '%s%f%f%f%*[^\n]', 'Headerlines', 1, 'Delimiter', ',');
fclose(fid);
Latitude=Data{3}';
Longitude=Data{4}';
Can i please know what is wrong?
1 Comment
Jim O'Doherty
on 23 Jun 2015
Hi, I managed to get this to read in as strings rather than floats. And not using the \n modifier
Data = textscan(fid, '%s%s%s%s', 'Headerlines', 1, 'Delimiter', ',');
Answers (1)
Stephen23
on 23 Jun 2015
Edited: Stephen23
on 23 Jun 2015
textscan works okay on that sample file. If you are having problems perhaps there is something in the real data file that does not match this format?
fid = fopen('actual_tgw2639true.csv','rt');
C = textscan(fid,'%s%f%f%f', 'Delimiter',',', 'HeaderLines',1);
fclose(fid);
which reads the dates as strings, and the rest of the data as floating point:
>> latitude = C{3}
latitude =
12.6600
10.9106
10.2050
8.9828
8.5950
7.5167
6.7567
6.5033
6.1817
4.4983
3.3567
3.1450
2.7250
2.3000
2.1667
1.9800
1.7367
1.6150
1.5222
>> longitude = C{4}
longitude =
83.5567
87.1264
88.5300
91.1714
91.9967
94.4167
95.9767
96.4917
97.5850
99.5300
100.9567
101.2200
101.7383
102.0050
102.0867
102.4350
102.8917
103.1200
103.1811
The date can be converted to a matrix of date vectors using datevec (and similarly to serial date numbers using datenum):
>> datevec(C{1},'dd/mm/yyyy HH:MM')
ans =
2015 2 5 18 57 0
2015 2 5 19 28 0
2015 2 5 19 40 0
2015 2 5 20 3 0
2015 2 5 20 10 0
2015 2 5 20 31 0
2015 2 5 20 44 0
2015 2 5 20 49 0
2015 2 5 20 58 0
2015 2 5 21 19 0
2015 2 5 21 33 0
2015 2 5 21 36 0
2015 2 5 21 41 0
2015 2 5 21 45 0
2015 2 5 21 47 0
2015 2 5 21 50 0
2015 2 5 21 54 0
2015 2 5 21 56 0
2015 2 5 21 57 0
0 Comments
See Also
Categories
Find more on Text Files 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!