Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Read numbers from a cell string and convert them -- improvement?

Subject: Read numbers from a cell string and convert them -- improvement?

From: Hsien-Wen

Date: 27 Sep, 2013 16:59:51

Message: 1 of 3

Hi all,

  I am processing 30 drifters' track files in csv format which contain time, longitude and latitude data as well as non-English characters. I used csvimport.m from Mathworks File Exchange to read data into a character cell (one cell for one file). I list two lines of data as below:(The 2nd item contains non-English characters)

CGA001,30分鐘回報,2012/07/19 00:47:54,23 22.889'N,119 34.277'E,26.633,12.1,349.8,03.2
CGA001,30分鐘回報,2012/07/19 00:17:54,23 21.323'N,119 34.584'E,26.833,12.1,003.1,02.7

Now I'd like to read out the latitude, which is in the 4th column of the data cell, and convert them into degree format. The codes I wrote were:

lat=data(end:-1:2,4);%read lat from the bottom of cell
lat_deg=cellfun(@(x) {x(1:2)},lat, 'UniformOutput', false); %read degree
nlat_deg=cellfun(@str2num, [lat_deg{:}]');%convert to number; or use str2double?
lat_min=cellfun(@(x) {x(4:9)},lat, 'UniformOutput', false); %read minute
nlat_min=cellfun(@str2num,[lat_min{:}]');%convert to number
ylat=nlat_deg+nlat_min/60.; %convert deg and min to degree

Although it works, it took me 6 lines to finish the job for latitude, then again for longitude. Since I am not familiar with cellfun operation, I am hoping for some advice to improve the coding about this work. Thanks in advance!

Subject: Read numbers from a cell string and convert them -- improvement?

From: dpb

Date: 29 Sep, 2013 21:19:04

Message: 2 of 3

On 9/27/2013 11:59 AM, Hsien-Wen wrote:
> Hi all,
>
> I am processing 30 drifters' track files in csv format which contain
> time, longitude and latitude data ...
> ...I list two lines of data as below: ...

...[shortened records for brevity]...

> CGA001,...,23 22.889'N,119 34.277'E,26.633,...
> CGA001,...,23 21.323'N,119 34.584'E,26.833,...
>
> .. read out the latitude, which is in the 4th column of
> the data cell, and convert them into degree format. The codes I wrote
> were:
>
> lat=data(end:-1:2,4);%read lat from the bottom of cell
> lat_deg=cellfun(@(x) {x(1:2)},lat, 'UniformOutput', false);
> nlat_deg=cellfun(@str2num, [lat_deg{:}]');%convert to number;
> lat_min=cellfun(@(x) {x(4:9)},lat, 'UniformOutput', false);
> nlat_min=cellfun(@str2num,[lat_min{:}]');
> ylat=nlat_deg+nlat_min/60.;
>
> Although it works, it took me 6 lines to finish the job for
> latitude, then again for longitude...

Since it's fixed length format, probably the simpler is just go ahead
and convert to character array and the process the substrings directly.

Sotoo

 >> s=char(c);
 >> lat=str2num(s(:,35:36))+str2num(s(:,38:43))/60
lat =
    23.3815
    23.3554
 >> lon=str2num(s(:,47:49))+str2num(s(:,51:56))/60
lon =
   119.5713
   119.5764
 >>

If either minute field can change an have 3 (or only 2) places you're in
deeper trouble...

--

Subject: Read numbers from a cell string and convert them -- improvement?

From: Hsien-Wen

Date: 1 Oct, 2013 01:47:23

Message: 3 of 3

dpb於 2013年9月30日星期一UTC+8上午5時19分04秒寫道:
> On 9/27/2013 11:59 AM, Hsien-Wen wrote:
>
> > Hi all,
>
> >
>
> > I am processing 30 drifters' track files in csv format which contain
>
> > time, longitude and latitude data ...
>
> > ...I list two lines of data as below: ...
>
>
>
> ...[shortened records for brevity]...
>
>
>
> > CGA001,...,23 22.889'N,119 34.277'E,26.633,...
>
> > CGA001,...,23 21.323'N,119 34.584'E,26.833,...
>
> >
>
> > .. read out the latitude, which is in the 4th column of
>
> > the data cell, and convert them into degree format. The codes I wrote
>
> > were:
>
> >
>
> > lat=data(end:-1:2,4);%read lat from the bottom of cell
>
> > lat_deg=cellfun(@(x) {x(1:2)},lat, 'UniformOutput', false);
>
> > nlat_deg=cellfun(@str2num, [lat_deg{:}]');%convert to number;
>
> > lat_min=cellfun(@(x) {x(4:9)},lat, 'UniformOutput', false);
>
> > nlat_min=cellfun(@str2num,[lat_min{:}]');
>
> > ylat=nlat_deg+nlat_min/60.;
>
> >
>
> > Although it works, it took me 6 lines to finish the job for
>
> > latitude, then again for longitude...
>
>
>
> Since it's fixed length format, probably the simpler is just go ahead
>
> and convert to character array and the process the substrings directly.
>
>
>
> Sotoo
>
>
>
> >> s=char(c);
>
> >> lat=str2num(s(:,35:36))+str2num(s(:,38:43))/60
>
> lat =
>
> 23.3815
>
> 23.3554
>
> >> lon=str2num(s(:,47:49))+str2num(s(:,51:56))/60
>
> lon =
>
> 119.5713
>
> 119.5764
>
> >>
>
>
>
> If either minute field can change an have 3 (or only 2) places you're in
>
> deeper trouble...
>
>
>
> --

Sorry, I did not make it clear. The non-English parts were not fixed length for all lines. At first I used textscan with %s to read each item of every line. However, textscan seemed not accept non-English characters as strings. It resulted in one cell with empty matrices. So I changed to cvsimport.
 
I was thinking to shorten my multiple-line command, and you've made an example for me. Thanks anyway!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us