How to Assign a Struct Field to a Matrix?

How can it assign field C and field D to the first and second rows of G, respectively?
A.B.C = [1 2 3];
A.B.D = [3 4 5];
Whend I do the following, I get the error „Subscripted assignment dimension mismatch.“
G(1, :) = A.B.C
G(2, :) = A.B.D

 Respuesta aceptada

dpb
dpb el 4 de Mzo. de 2017
Editada: dpb el 4 de Mzo. de 2017
'Cuz you must have an already-existing G variable...
>> g=pi;
>> g(1,:)=A.B.C
Subscripted assignment dimension mismatch.
>>
>> clear g % now ensure no previous variable exists first...
>> g(1,:)=A.B.C
g =
1 2 3
>> g(2,:)=A.B.D
g =
1 2 3
3 4 5
>>
works just fine if don't have conflicting definition already in workspace. But,
>> g=pi;
>> g=[A.B.C;A.B.D]
g =
1 2 3
3 4 5
>>
is simpler and overwrites the previous g silently even though existed.
However, in general, this looks like a bad road to start down; soon you'll be having issues with using sequential-named fields.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Mzo. de 2017

Editada:

dpb
el 4 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by