Azzera filtri
Azzera filtri

structfun output array not working

3 visualizzazioni (ultimi 30 giorni)
Hassan Aleem
Hassan Aleem il 15 Mar 2016
Risposto: George Abrahams il 30 Dic 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

Risposte (3)

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

Accedi per commentare.


Stephen23
Stephen23 il 15 Mar 2016
structfun is working fine. The problem is how you are using it. Try this:
vec = structfun(@numel,timelockdata);
  2 Commenti
Hassan Aleem
Hassan Aleem il 15 Mar 2016
this just puts the values into one variable named vec.
Stephen23
Stephen23 il 16 Mar 2016
Modificato: Stephen23 il 17 Mar 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.

Accedi per commentare.


George Abrahams
George Abrahams il 30 Dic 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

Community Treasure Hunt

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

Start Hunting!

Translated by