Borrar filtros
Borrar filtros

structfun output array not working

3 visualizaciones (últimos 30 días)
Hassan Aleem
Hassan Aleem el 15 de Mzo. de 2016
Respondida: George Abrahams el 30 de Dic. de 2022
timelockdata.m0='timelock_m0';
timelockdata.m6='timelock_m6';
timelockdata.m3w='timelock_m3w';
timelockdata.m3b='timelock_m3b';
timelockdata.same='timelock_same';
timelockdata.diff='timelock_diff';
[m0,m6,m3w,m3b,same,diff]=structfun(@numel,timelockdata);
results in:
Error using numel
Too many output arguments.
How can I get the results of structfun to save in the name of the corresponding variables

Respuestas (3)

Stalin Samuel
Stalin Samuel el 15 de Mzo. de 2016
s=structfun(@numel,timelockdata,'UniformOutput',0)
  1 comentario
Hassan Aleem
Hassan Aleem el 15 de Mzo. de 2016
This does not work. gives the same answer.It is a uniform output by the way.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 15 de Mzo. de 2016
structfun is working fine. The problem is how you are using it. Try this:
vec = structfun(@numel,timelockdata);
  2 comentarios
Hassan Aleem
Hassan Aleem el 15 de Mzo. de 2016
this just puts the values into one variable named vec.
Stephen23
Stephen23 el 16 de Mzo. de 2016
Editada: Stephen23 el 17 de Mzo. de 2016
You have called structfun with numel, which only returns one output. The output is one variable. It does not magically become lots of separate variables as you seem to want. This is clearly explained in the structfun documentation. As I already said in my answer "The problem is how you are using it".
If you want vec allocated into multiple variables, then you have to do this yourself afterwards:
>> vec = 1:5
vec =
1 2 3 4 5
>> tmp = num2cell(vec);
>> [S.a,S.b,S.c,S.d,S.e] = tmp{:};
>> S
S =
a: 1
b: 2
c: 3
d: 4
e: 5
Personally I would keep them in one variable, because that makes data processing much simpler.

Iniciar sesión para comentar.


George Abrahams
George Abrahams el 30 de Dic. de 2022
Old question, but my fieldfun function on File Exchange / GitHub will process the fields of structure timelockdata and output a structure with the same fields.
timelockdata = struct('m0','timelock_m0','m6','timelock_m6','m3w',...
'timelock_m3w','m3b','timelock_m3b','same','timelock_same',...
'diff','timelock_diff');
fieldfun( @numel, timelockdata )
% ans = struct with fields:
% m0: 11
% m6: 11
% m3w: 12
% m3b: 12
% same: 13
% diff: 13
This is most likely superior to assigning the values to individual variables, which is exactly what you requested, as you don't need to hardcode the field names, so it will adjust if you change structure.
If you want to do that anyway, Stephen's answer essentially gave you the method: use Comma-Separated Lists.
timelocknumel = num2cell( structfun( @numel, timelockdata ) );
[ m0, m6, m3w, m3b, same, diff ] = timelocknumel{:}
% m0 = 11
% m6 = 11
% m3w = 12
% m3b = 12
% same = 13
% diff = 13

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by