Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Converting a Column of data into 3 columns

Subject: Converting a Column of data into 3 columns

From: Nicole Sharp

Date: 2 Jul, 2008 01:33:05

Message: 1 of 4

Hi all,
I'm working with a large dataset that has multiple columns
of data. The one column contains the year, month, and day in
the following format:

20080701

My question is how can I break up this column so that when I
read in the data from my text file that the month year and
data are separate variables?

Any help would be appreciated.
-N

Subject: Converting a Column of data into 3 columns

From: Nitin Chhabra

Date: 2 Jul, 2008 03:43:02

Message: 2 of 4

"Nicole Sharp" <nicolesharp@yahoo.com> wrote in message
<g4elsh$1gs$1@fred.mathworks.com>...
> Hi all,
> I'm working with a large dataset that has multiple columns
> of data. The one column contains the year, month, and day
in
> the following format:
>
> 20080701
>
> My question is how can I break up this column so that
when I
> read in the data from my text file that the month year and
> data are separate variables?
>
> Any help would be appreciated.
> -N
Hi,

Here is the way :
Read the column as string variable.convert in char and take
desired index.

>> a='20080712'

a =

20080712

>> b=char(a)

b =

20080712

>> b(1:4)

ans =

2008

>> b(5:6)

ans =

07

>> b(7:8)

ans =

12

with regards,
Nitin



Subject: Converting a Column of data into 3 columns

From: Ashwini Deshpande

Date: 2 Jul, 2008 04:55:03

Message: 3 of 4

"Nicole Sharp" <nicolesharp@yahoo.com> wrote in message
<g4elsh$1gs$1@fred.mathworks.com>...
> Hi all,
> I'm working with a large dataset that has multiple columns
> of data. The one column contains the year, month, and day in
> the following format:
>
> 20080701
>
> My question is how can I break up this column so that when I
> read in the data from my text file that the month year and
> data are separate variables?
>
> Any help would be appreciated.
> -N

Hai,

help datestr

Ashwini

Subject: Converting a Column of data into 3 columns

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 2 Jul, 2008 06:16:19

Message: 4 of 4

In article <g4elsh$1gs$1@fred.mathworks.com>,
Nicole Sharp <nicolesharp@yahoo.com> wrote:
>I'm working with a large dataset that has multiple columns
>of data. The one column contains the year, month, and day in
>the following format:

>20080701

>My question is how can I break up this column so that when I
>read in the data from my text file that the month year and
>data are separate variables?

If the dataset is already in memory, and is in character format,
then the earlier answer about using array indices is the appropriate
answer.

If it is already in memory and is in numeric format, then
datestr as suggested by another poster might be convenient, but
since it is a large array you might find it much more efficient
to use mod() and rem() and similar, along the line of

day = mod(date,100);
month = mod(date,1000)-day;
year = floor(date / 1000);

If the dataset is being read in from a text file, then at the
time you are reading it in, instead of using a %d or %f format,
use %4d%2d%2d to create 3 numeric variables converted from
4 characters, 2 characters, and 2 characters respectively.
--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them." -- George Orwell

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics