store data from matrix rowwise in indexed structure

Hello everyone, I have a structure 'Sample', which contains many objects (over 1000) with the same attributes. For example sample specific data is stored as a 1x2090 vector under Sample([]).YData. If I want to extract the 'YData' of sample 1 to 151 and store it in a matrix, I simply write
Select = 1:151; YData = vertcat(Sample(Select).YData);
Now I want to write selected data back to structure... Each row of a matrix should be written to the 'YData' of the corresponding sample.
Sample(Select).YData = Matrix;
doesn't work. Is there a way to implement this task in such a simple way or will only a for-loop do this job? Thanks

 採用された回答

Guillaume
Guillaume 2016 年 5 月 10 日
編集済み: Guillaume 2016 年 5 月 10 日

1 投票

To create your matrix in the first place you went through two steps:
  1. Created a comma separated list out of your structure (simply by writing Sample(Select).YData)
  2. Concatenated together all the elements of the list into one matrix with vertcat
To do the inverse you have to reverse these steps
  1. Split the matrix into rows
  2. deal these rows to the structure
Unfortunately, it can't be done as a one-liner, you have to use a temporary variable
rows = num2cell(Matrix, 2); %split matrix into rows
[Sample(Select).YData] = rows{:}; %distribute rows into structure

3 件のコメント

Andrei Bobrov
Andrei Bobrov 2016 年 5 月 10 日
I agree with Guillaume. I deleted my answer.
Doreen
Doreen 2016 年 5 月 10 日
Great :-) Many thanks!
Andrei Bobrov
Andrei Bobrov 2016 年 5 月 10 日
But working in Octave:
Sample(Select) = cell2struct(num2cell(Matrix,2),{'YData'},2);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 5 月 10 日

コメント済み:

2016 年 5 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by