# How can I assign several values from cell-array tovariables?

3 vues (au cours des 30 derniers jours)
Diego R le 4 Sep 2019
I have the cell array from the example:
myNum = [1, 2, 3]; myCell = {'one', 'two'}; myStruct.Field1 = ones(3); myStruct.Field2 = 5*ones(5);
C = {myNum, 100*myNum; myCell, myStruct}
C = 2x2 cell array
{1x3 double} {1x3 double}
{1x2 cell } {1x1 struct}
a=1
b=2
c=3
I've tried to do these, but didn't work:
[a,b,c]=C{1,1}
[a,b,c]=deal(C{1,1}(1,:))
I need to do in one time because my real code has dozens of values.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Kelly Kearney le 4 Sep 2019
I think you'll need to use a quick intermediate variable so you can convert the numeric array to a comma-separated list:
tmp = num2cell(C{1,1});
[a,b,c] = deal(tmp{:})
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Star Strider le 5 Sep 2019
In more recent MATLAB releases, deal isn’t necessary.
Just use:
[a,b,c] = tmp{:}

Connectez-vous pour commenter.

### Plus de réponses (2)

Walter Roberson le 5 Sep 2019
Unfortunately the temporary variable is still a practical necessity. It can be done away with, at cost:
[a,b,c] = subsref(num2cell(C{1,1}), struct('type', '{}', 'subs', {{':'}}))
but it becomes a lot easier if you define
VECEXPAND = @(VEC) VEC{:};
DEALVEC = @(VEC) VECEXPAND(num2cell(VEC));
[a, b, c] = DEALVEC(C{1,1})
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Diego R le 5 Sep 2019
Thanks a lot. It works. But it is a shame there isn't a direct way.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by