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

27 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by