Borrar filtros
Borrar filtros

Create new fields on structure from char array

15 visualizaciones (últimos 30 días)
Mechrod
Mechrod el 4 de Mzo. de 2018
Comentada: Walter Roberson el 5 de Mzo. de 2018
Hi, I have an struct with some char names, and I want each one to became a field in other structure. This is the structure with the names:
This is the structure and the place where I want the names:
I tried several things from Matlab Answers, but nothing works. For example:
arbitrage = struct('Exchanges',list_exchanges);
for ii = 1:15
newfield = coins(ii).coins;
arbitrage.(newfield) = 1;
%arbitrage = setfield(arbitrage,'newfield',[]);
%[arbitrage(:).newfield] = [];
end
Presents this error:
Scalar structure required for this assignment.
Error in arbitrage_bot_v0_1 (line 21)
arbitrage.(newfield) = 1;
Can someone help me? Thanks.

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Mzo. de 2018
[arbitrage.(newfield)] = deal(1);
  2 comentarios
Mechrod
Mechrod el 5 de Mzo. de 2018
Its incredible how simple details make all the difference. It worked.
Could you quickly explain what was wrong and what your suggestion did? This may help anyone who appears here to check the answer. Thanks!
Walter Roberson
Walter Roberson el 5 de Mzo. de 2018
arbitrage is a nonscalar struct according to the second image header line. arbitrage.(newfield) would undergo expansion as a comma separated list, as that expansion is what is defined for nonscalar struct indexed at a field (or for using {} on a nonscalar cell array with a nonscalar index). To assign to all of the comma expanded fields at the same time you need to list them in [] on the left side of an assignment.
The second half of the trick is using deal() to assign the same value to multiple variables.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by