Assinging String Value to Structure

조회 수: 1 (최근 30일)
Nick G
Nick G 2015년 8월 31일
편집: Cedric 2015년 8월 31일
I'm having trouble assigning a string value ('practice') to every level of a structure called unshuff.
These work:
unshuff(1,1).myfield = 'practice';
unshuff(1,2).myfield = 'practice';
But this does not work:
unshuff(1,:).myfield = 'practice';
I get an error message that says:
Insufficient outputs from right hand side to satisfy comma separated list expansion on left hand side. Missing [] are the most likely cause.
How can one (efficiently) assign a string value to every level of a given field in a structure?

채택된 답변

Cedric
Cedric 2015년 8월 31일
Here is one way:
[unshuff(1,:).myfield] = deal( 'new practice' ) ;
  댓글 수: 2
Nick G
Nick G 2015년 8월 31일
Thanks.
Cedric
Cedric 2015년 8월 31일
편집: Cedric 2015년 8월 31일
You're welcome. Here is another way that you could profile if relevant:
newVals = repmat( {'new practice'}, 1, size( unshuff, 2 )) ;
[unshuff(1,:).myfield] = newVals{:} ;
To understand what we are doing in both cases, you have to understand Comma Separated Lists (CSL) in MATLAB.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by