2

I have a Cell Array 1x254 with data of this type:

data = {'15/13' '14/12' '16/13' '16/13' '16/14' '17/14' '17/14' '18/14' '19/15'};

the first number corresponds to the temp, the second number the temp2

I need to separate the data and insert them in a matrix :

B =

    15    13
    14    12
    16    13
    16    13
    16    14
    17    14
    18    14
    19    15

I tried to use this solution

data = regexp(tempr, '\W','split');
B=cell2mat(cat(3,data{:}));

but I find no way to get ahead....

could give me a hint?

1 Answer 1

3

You are pretty close. You can do it using regexp as you did, but with / as the delimiter, in addition to cellfun(which is just a loop really) to convert from strings to digits, then apply cell2mat to get a numeric array as output:

clc
clear

data = {'15/13' '14/12' '16/13' '16/13' '16/14' '17/14' '17/14' '18/14' '19/15'};

%// Split data
C = regexp(data, '/', 'split');

%// Convert from strings to double
D = cellfun(@str2double,C,'uni',0);

%// Get final numeric matrix
E = cell2mat([D(:)])

NOTE:

As pointed out by Luis Mendo, str2double operates on cell arrays so you can trade cellfun and cell2mat for this single line:

E = str2double(vertcat(C{:}))

Output:

E =

    15    13
    14    12
    16    13
    16    13
    16    14
    17    14
    17    14
    18    14
    19    15
Sign up to request clarification or add additional context in comments.

4 Comments

Great glad I could help!
str2double allows cell array input. So you can use E = str2double(vertcat(C{:}));
Oh right I keep forgetting that. I'll add it to the answer thanks!
@Luis MendoYou have always elegant solutions, thanks

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.