Assigning an Array to Structure Field

조회 수: 14 (최근 30일)
Nick G
Nick G 2015년 9월 1일
답변: Walter Roberson 2015년 9월 1일
I'm having trouble assigning an array to a structure field.
shuff = randperm(length(unshuff));
[unshuff.trial] = shuff;
I get the following error message:
Too many output arguments.
Any ideas?

답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 1일
If unshuff is an existing structure array, then
unshuff.trial
would expand to multiple locations as if you had written
[unshuff(1).trial, unshuff(2).trial, ... unshuff(end).trial] = shuff;
but your shuff is only a single output.
If you want to assign the same value to each of the locations, use
[unshuff.trial] = deal(shuff);
If you want to assign one of the values in shuff to each of the outputs then:
shuff = num2cell(randperm(length(unshuff)));
[unshuff.trial] = shuff{:}; %or deal(shuff{:})

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by