1

I have a cell array that is declared as follow :

testCellArray = cell(m, n);

In a script, I specify the value of each cell as follow :

for loop (ii)
    testCellArray{ii, 1} = str2num(value1{ii});
    testCellArray{ii, 2} = str2num(value2{ii});
    testCellArray{ii, 3} = str2num(value3{ii});
    testCellArray{ii, 4} = str2num(value4{ii});
    ...
    testCellArray{ii, n} = str2num(valuen{ii});
end

I'm unable to put this in one line (I'm pretty sure it is possible).

I've tried something like this :

testCellArray{ii, :} = deal(str2num(value1{ii}, value2{ii}, value3{ii}, value4{ii}, ..., valuen{ii}))

But it isn't working. I'm also not sure if I should use testCellArray{ii, :} or testCellArray(ii, :).

3 Answers 3

3

The better way would probably be the use of str2double instead of str2num, since it also takes a cell-array as input.

Than you could arrange the assignments as follows, without the outer ii loop:

testCellArray(:,1) = num2cell(str2double(value1));
testCellArray(:,2) = num2cell(str2double(value2));
...
testCellArray(:,n) = num2cell(str2double(valuen));

And, if your variables are really named value1, value2 etc., I can't resist to recommend: Get rid of enumerated variable names and put things into a list, presumably a cell-array in this case.

If you really need/want to keep the loop, the proper version of your deal assignment would be:

[testCellArray{ii, :}] = deal(str2num(value1{ii}), str2num(value2{ii}), ..., str2num(valuen{ii}));

However, not knowing how large n is, putting this in one gigantic line instead of several shorter ones surely isn't good coding style.

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

1 Comment

Right now n = 16. And no, the variables are not really named value1, value2, ...etc.
2

Assuming value1, value2 etc are vertical cell arrays of strings:

testCellArray = num2cell([str2num(strvcat(value1{:})) str2num(strvcat(value2{:}))]);

Note that strvcat can handle strings witth different lengths.

Example:

>> value1 = {'1';'2';'3';'14'};
>> value2 = {'5';'6';'7';'182'};
>> testCellArray = num2cell([str2num(strvcat(value1{:})) str2num(strvcat(value2{:}))])

testCellArray = 

    [ 1]    [  5]
    [ 2]    [  6]
    [ 3]    [  7]
    [14]    [182]

It's much more convenient if all value1, value2 etc are lumped into a single 2D cell array, such as:

value = {'1'  '5'   '-1';
         '2'  '6'   '-2';
         '3'  '7'   '-3';
         '14' '182' '-4'}; %// arbitrary number of columns

That way you don't have to modify the code to accomodate a varying number of columns. Just use

testCellArray = num2cell(reshape(str2num(strvcat(value{:})), size(value)));

or equivalently

testCellArray = mat2cell(str2double(value), ones(1,size(value,1)), ones(1,size(value,2)) );

to obtain the result

testCellArray = 

    [ 1]    [  5]    [-1]
    [ 2]    [  6]    [-2]
    [ 3]    [  7]    [-3]
    [14]    [182]    [-4]

1 Comment

The fact is that the lenght of value1, value2, ..., valuen are not the same. So I don't see how they could be put in a single 2D cell array.
1

Solution with only one instruction:

testCellArray = cellfun(@str2num, [value1(:),value2(:),value3(:),...
  ,valuen(:)],'UniformOutput',false));

If all value* are vectical vectors, you can take (:) out.

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.