0

If I have a Matlab CELL line of string characters separated by a comma like:

12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C

How do I separate each one into its own column within a matrix? What is the most efficient way of doing this? Can this be done? Thanks

1

2 Answers 2

1

UPDATE: Just realized this question duplicates how-to-convert-comma-separated-string-to-cell-array-of-strings-in-matlab and that my answer duplicates the answer provided by @Jonas. I'm flagging it as a duplicate.

Here is the most efficient method I'm aware of:

%# Build your string
Input = '12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C';

%# Convert to cell array of strings
Output = regexp(Input, '([^,]*)', 'tokens');
Output = cat(2, Output{:});

Some points to note:

1) Note, this will work whether Input is a character array (ie a string) or a cell containing a character array.

2) I've assumed you want the output to be a cell array of strings. Offhand, I can't think of another way of grouping strings of differing length into separate columns using Matlab without building your own custom class.

3) The solution provided by @IlyaKobelevskiy will group the strings into separate rows (not columns) and thus essentially limits you to only a single observation.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming str is your cell line:

str ={'12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C'};
ind=strfind(str{1},',');
len=diff([0 ind]);
sz=max(len);
A=zeros(length(ind),sz);
prev=1;
for i=1:length(ind)
    A(i,1:len(i)-1)=str{1}(prev:(ind(i)-1));
    prev=ind(i)+1;
end;
A=char(A);

Not sure if it is the most efficient way, but should work...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.