필터 지우기
필터 지우기

structfun output array not working

조회 수: 2 (최근 30일)
Hassan Aleem
Hassan Aleem 2016년 3월 15일
답변: George Abrahams 2022년 12월 30일
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

답변 (3개)

Stalin Samuel
Stalin Samuel 2016년 3월 15일
s=structfun(@numel,timelockdata,'UniformOutput',0)
  댓글 수: 1
Hassan Aleem
Hassan Aleem 2016년 3월 15일
This does not work. gives the same answer.It is a uniform output by the way.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2016년 3월 15일
structfun is working fine. The problem is how you are using it. Try this:
vec = structfun(@numel,timelockdata);
  댓글 수: 2
Hassan Aleem
Hassan Aleem 2016년 3월 15일
this just puts the values into one variable named vec.
Stephen23
Stephen23 2016년 3월 16일
편집: Stephen23 2016년 3월 17일
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.

댓글을 달려면 로그인하십시오.


George Abrahams
George Abrahams 2022년 12월 30일
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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by