How to Change Values of a Field by an Indexing Array?

3 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 3 月 12 日
編集済み: Jan 2017 年 3 月 13 日
Here’s my initial struct:
A(1).B.C = 'a';
A(2).B.C = 'b';
A(3).B.C = 'a';
A(4).B.C = 'a';
I want to change the values of C based on values of Values and indexes of IndexingArray:
Values = {'a', 'b'}
IndexingArray = [1 1 0 1];
So, my struct will be:
A(1).B.C = 'b';
A(2).B.C = 'b';
A(3).B.C = 'a';
A(4).B.C = 'b';
What is the solution?


Chad Greene
Chad Greene 2017 年 3 月 12 日
You could change each element individually via loop. But make sure you add 1 to the indexing array values because Matlab starts indexing at 1, not 0.
for k = 1:length(A)
A(k).B.C = Values(IndexingArray(k)+1);
  3 件のコメント
Chad Greene
Chad Greene 2017 年 3 月 13 日
Ah, yes, curly braces. Thanks for catching that Jan!


その他の回答 (0 件)


Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by