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:
Converting a string cell to a matrix

Subject: Converting a string cell to a matrix

From: Hanah

Date: 3 Sep, 2010 18:46:04

Message: 1 of 7

Friends,

If I have a {5x1 cell} string array, C with the following comma-separated content:

MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282

Is there a simple way to convert this to a 5x10 matrix, M of the respective data types?

For example, the first row (1x1 string array) of:
MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189

Becomes a (1x10 matrix) of string, date/time, string, date, integer, char, float, etc.. data:
[MSFT, 2/1/2010 9:31, MSQ, 2/20/2010, 25, c, 3.4, 3.4, 3.4, -0.01189]

I've tried str2mat and other related functions to try to separate the data by reading the ',' character but no luck! Thanks for any help.

Han

Subject: Converting a string cell to a matrix

From: B. Schmidt

Date: 3 Sep, 2010 19:33:03

Message: 2 of 7

"Hanah " <zadonix@yahoo.com> wrote in message <i5rfpc$2ff$1@fred.mathworks.com>...
> Friends,
>
> If I have a {5x1 cell} string array, C with the following comma-separated content:
>
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282
>
> Is there a simple way to convert this to a 5x10 matrix, M of the respective data types?
>
> For example, the first row (1x1 string array) of:
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
>
> Becomes a (1x10 matrix) of string, date/time, string, date, integer, char, float, etc.. data:
> [MSFT, 2/1/2010 9:31, MSQ, 2/20/2010, 25, c, 3.4, 3.4, 3.4, -0.01189]
>
> I've tried str2mat and other related functions to try to separate the data by reading the ',' character but no luck! Thanks for any help.
>
> Han

You could try saving your matrix to text file and the use the "csvread" or "textscan" functions to read the file back into MATLAB. To write to the file you can use the "fprintf" function. It's not exactly very efficient but it should work.

Some example code:

        fid = fopen(filepath, 'w');
        for i = 1: 5
               fprintf(fid, '%s\r\n', yourcellarray{i,:});
        end
        fclose(fid);

        [a b c d e f g h] = csvread(filepath)

*filepath is a string like "C:\temp\mynewfile.txt"

Subject: Converting a string cell to a matrix

From: Walter Roberson

Date: 3 Sep, 2010 19:37:05

Message: 3 of 7

On 10-09-03 01:46 PM, Hanah wrote:

> If I have a {5x1 cell} string array, C with the following
> comma-separated content:
>
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282
>
> Is there a simple way to convert this to a 5x10 matrix, M of the
> respective data types?
>
> For example, the first row (1x1 string array) of:
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
>
> Becomes a (1x10 matrix) of string, date/time, string, date, integer,
> char, float, etc.. data:
> [MSFT, 2/1/2010 9:31, MSQ, 2/20/2010, 25, c, 3.4, 3.4, 3.4, -0.01189]
>
> I've tried str2mat and other related functions to try to separate the
> data by reading the ',' character but no luck! Thanks for any help.

No. You are assuming here that variables of different data type can be mixed
within the same array. That is not possible. The closest to that is that if
you use a cell array, each cell can be a different type

{'MSFT', '2/1/2010 9:31', 'MSQ', '2/20/2010', int32(25), 'c', 3.4, 3.4, 3.4,
-0.1189}

If you join all the entries together in to one string row with any "end of
row" delimiter between them, then you can use textscan on the whole thing at
once. textscan() will, however, output a cell array with one cell per column.

Subject: Converting a string cell to a matrix

From: B. Schmidt

Date: 3 Sep, 2010 19:40:23

Message: 4 of 7

"B. Schmidt" <bschmidt0123@gmail.com> wrote in message <i5rihf$51q$1@fred.mathworks.com>...
> "Hanah " <zadonix@yahoo.com> wrote in message <i5rfpc$2ff$1@fred.mathworks.com>...
> > Friends,
> >
> > If I have a {5x1 cell} string array, C with the following comma-separated content:
> >
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282
> >
> > Is there a simple way to convert this to a 5x10 matrix, M of the respective data types?
> >
> > For example, the first row (1x1 string array) of:
> > MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
> >
> > Becomes a (1x10 matrix) of string, date/time, string, date, integer, char, float, etc.. data:
> > [MSFT, 2/1/2010 9:31, MSQ, 2/20/2010, 25, c, 3.4, 3.4, 3.4, -0.01189]
> >
> > I've tried str2mat and other related functions to try to separate the data by reading the ',' character but no luck! Thanks for any help.
> >
> > Han
>
> You could try saving your matrix to text file and the use the "csvread" or "textscan" functions to read the file back into MATLAB. To write to the file you can use the "fprintf" function. It's not exactly very efficient but it should work.
>
> Some example code:
>
> fid = fopen(filepath, 'w');
> for i = 1: 5
> fprintf(fid, '%s\r\n', yourcellarray{i,:});
> end
> fclose(fid);
>
> [a b c d e f g h] = csvread(filepath)
>
> *filepath is a string like "C:\temp\mynewfile.txt"

Sorry I forgot, but you have to use the "textscan" function instead of csvread because the latter only allows numeric comma separtated value text files. It looks like this:

        fid1 = fopen(fileloc,'r');
        formatstring = '%s';
        delimstring = ',';
        newcells = textscan(fid1,formatstring,'BufSize',32768,'Delimiter',delimstring);
        fclose(fid1);

Subject: Converting a string cell to a matrix

From: Hanah

Date: 3 Sep, 2010 20:45:05

Message: 5 of 7

Hi B. Schmidt,

Thanks much for the help. But that's the problem. The cell array is actually the output of a textscan operation on a .csv file, so it would be akin to me chasing my tail.

The original problem is this.. I have a collection of cvs files containing 72450 x 50 cells of alpha numeric data, and I would like to read those into a matrices.

Suppose I have a 5 row X 10 column csv file with one headerline (commas denote different columns):

MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282

How can I convert this to a 5 X 10 matrix, while maintaining the integrity of the data types?

..
Initially, I tried an operation like this:

fid = fopen('input.csv','rt');
C = textscan(fid,'%s','delimiter','[]','headerlines',1)
fclose(fid);
data = cat(1, C{:});
string_data = regexp(data,'([A-Z a-z]+)','match');
string_data = cat(1,string_data{:});
numeric_data = regexp(data,'([0.00-9.99]+)','match');
numeric_data = cat(1,numeric_data{:});

But I wound up with 5 rows and 17 columns of string data.. not a solution.
..
Then I tried this approach:

fid = fopen('input.csv','rt');
C = textscan(fid,'%s%s%s%s%d8%c%f%f%f%f%*[^\n] ','delimiter',',','headerlines',1, 'CollectOutput', true);
fclose(fid);
%I wind up with a 1 x 4 cell array C, and try to group and concatenate them:
grp1 = cat(1,C{1});
grp2 = cat(1,C{2});
grp3 = cat(1,C{3});
grp4 = cat(1,C{4});
M = [grp1 grp2 grp3 grp4];

This should be an frictionless solution, but for some reason, I get the error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Isn't there a simple way to read alphanumeric data into a matrix to enable operations on specified rows? I seem to have hit an inexplicable road block. Is there something I am not seeing? Thanks much for the your kind consideration, time and help!

Han

Subject: Converting a string cell to a matrix

From: Hanah

Date: 3 Sep, 2010 20:55:22

Message: 6 of 7

Hi Walter,

The original problem was to for example, convert a 5 row X 10 column csv file with one headerline (commas denote different columns):

MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282

To a 5 x 10 matrix with of this data while retaining the data types. The reason for this is to make mathematical operations of this data uncomplicated.

If I understand you correctly, do you imply that I can only have this as a cell array? If that is the case, is there a way to reference a specific row within the cell, and perform a mathematical operation of one of the elements/columns of that row. The cell arrays appear to make such operations unattainable. For instance, how can I possibly derive a 5 x 10 cell array of this data?

Thank you Walter, for all the help.

Hanna

Subject: Converting a string cell to a matrix

From: Walter Roberson

Date: 3 Sep, 2010 23:13:43

Message: 7 of 7

On 10-09-03 03:45 PM, Hanah wrote:

> Suppose I have a 5 row X 10 column csv file with one headerline (commas
> denote different columns):
>
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,c,3.4,3.4,3.4,-0.01189
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,25,p,0,0,0,0.006922
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,c,0,0,0,-0.013851
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,26,p,0.11,0.11,0.11,0.011821
> MSFT,2/1/2010 9:31,MSQ,2/20/2010,27,c,0,0,0,1.00282
>
> How can I convert this to a 5 X 10 matrix, while maintaining the
> integrity of the data types?

> Initially, I tried an operation like this:
>
> fid = fopen('input.csv','rt');
> C = textscan(fid,'%s','delimiter','[]','headerlines',1)

C = textscan(fid, '%s%s%s%s%d%s%f%f%f%f', 'delimiter', ',', 'headerlines, 1);

> fclose(fid);

C will now be a 1 x 10 cell array where each entry contains a column's-worth
of data.

 > If I understand you correctly, do you imply that I can only have this as a
 > cell array? If that is the case, is there a way to reference a specific row
 > within the cell, and perform a mathematical operation of one of the
 > elements/columns of that row. The cell arrays appear to make such
 > operations unattainable. For instance, how can I possibly derive a 5 x 10
 > cell array of this data?

If you had the array D that you wanted, then to get to column c of row r, you
would index D{r,c}. With the above cell array C, you would instead index
D{c}(r) . The result would be a single numeric value for the numeric
positions, and would be a cell array containing a string for the string
positions; another way of putting that is that
D{c}(r) would be appropriate for the numeric locations and
D{c}{r} would be appropriate for the strings.

The difference compared to D{r,c} is minor and uninteresting unless you need
to rebuild a row for some reason.

But if you insist...

t = arrayfun(@(J) {arrayfun(@(K) C{K}(J), ...
1:size(C,2),'Uniform',0)},1:size(C{1},1));
D = vertcat(t{:});

D will now be a 5 x 10 cell array. D{r,c} will index row r, column c. If the
value there is numeric, D{r,c} will be the scalar numeric answer, but if the
value there is a string, D{r,c} will be a cell array containing a single
string and so D{r,c}{1} would be needed to get to the string contents.

Tags for 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