How can I fill a structure array with a scalar array?

조회 수: 23 (최근 30일)
Giuela
Giuela 2017년 5월 26일
편집: Stephen23 2019년 10월 21일
I have a structure array like this:
myStruct=repmat(struct('field1',0),10,1);
and I have a double array:
myarray = [1 2 3 4 5 6 7 8 9 10];
If I wanto to initialize mystruct with my array I have to do this>
for i = 1:10 mystruct(ii).field1 = myarray(ii); end
There is a way to do this with one row of statement?
If I do
mystruct.field1 = myarray
I get the error:
"Scalar structure required for this assignment"
I've also tried:
mystruct(:).field1 = myarray(:)
but the error was: "Expected one output from a curly brace or dot indexing expression, but there were 10 results."
what can I do?

채택된 답변

Stephen23
Stephen23 2017년 5월 26일
편집: Stephen23 2017년 5월 26일
If you have a non-scalar structure and that you want to allocate new data to the same field of each element of that structure, then this is easiest using a comma-separated list:
>> myStruct=repmat(struct('oldfield',0),10,1);
>> myarray = [1,2,3,4,5,6,7,8,9,10];
>> C = num2cell(myarray);
>> [myStruct.newfield] = C{:};
  댓글 수: 3
Stephen23
Stephen23 2017년 5월 26일
편집: Stephen23 2019년 10월 21일
@Maurizio: I am glad to help. I recommend reading the documentation that I linked to.
Giuela
Giuela 2017년 5월 26일
I agree with you, Stephen, but often the documentation doesn't help so much, I haven't found any example similar to my problem, I've tried to solve it using my knowledge, but I'm not so expert to find the solution.

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

추가 답변 (2개)

utsav kakkad
utsav kakkad 2018년 10월 21일
no joy by this method too ........any alternative?

utsav kakkad
utsav kakkad 2018년 10월 22일
this can work out if you can assign values to it manually by typing it out from the key board but when you have to make an assignment programmatically what shall you do? I am working on one such problem right now: I have a structure and I need to assign a value to it , I cannot make a comma separated list here. Any suggestions? Matlab documentation on structures doesn't help me out
  댓글 수: 1
Stephen23
Stephen23 2018년 10월 24일
편집: Stephen23 2018년 10월 24일
"I cannot make a comma separated list here."
There are many possible combinations of structure and array:
  • assign one scalar array to one field of a scalar structure.
  • assign one non-scalar array to one field of a scalar structure.
  • assign one non-scalar array to separate fields of a scalar structure.
  • assign elements of one array to separate fields of a scalar structure.
  • assign one scalar array to one field of a non-scalar structure.
  • assign elements of one non-scalar array to one field of a non-scalar structure.
  • ... etc.
Some of these can be solved using a comma-separated list, whereas some of them require other syntaxes. However you did not give us any actual information on your structure, on your array, and on exactly which elements you want to assign and in what way. This makes it very hard for us to help you.
"Any suggestions?"
Give us example data, and/or an actual description of your data, and also explain exactly how you want the data to be assigned to the structure.

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

카테고리

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