フィルターのクリア

how distribuite correctly array in struct

2 ビュー (過去 30 日間)
piero
piero 2023 年 11 月 8 日
コメント済み: piero 2023 年 11 月 8 日
vb=[Sis.dailyprof];
[Sis.dailyprof_Orig]=deal(vb);
but this is not correct...i want to get dailyprof_Orig similar to dailyprof (5787X1 for each element)

採用された回答

Walter Roberson
Walter Roberson 2023 年 11 月 8 日
%sample data
Sis = struct('dailyprof', {19, [3 11], -5})
Sis = 1×3 struct array with fields:
dailyprof
%do the actual work
[Sis.dailyprof_Orig] = Sis.dailyprof;
%cross-chedk
Sis
Sis = 1×3 struct array with fields:
dailyprof dailyprof_Orig
Sis(2).dailyprof
ans = 1×2
3 11
Sis(2).dailyprof_Orig
ans = 1×2
3 11
  3 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 8 日
When you call struct() and give a field name, then if the parameter after that in the struct() call is not a cell array, then you are defining a scalar struct and the value of the parameter is set for the field in the scalar struct. For example,
struct('field1', [3 5 9])
ans = struct with fields:
field1: [3 5 9]
So you got out a scalar struct and the non-cell content [3 5 9] was assigned to the field. Notice you did not get out a 3 x 1 struct array with field1 being "3" in one entry, "5" in another, and "9" in the third entry.
Here, [ ] is just standard list-building syntax,
A = [3 5 9]
A = 1×3
3 5 9
B = horzcat(3, 5, 9)
B = 1×3
3 5 9
isequal(A, B)
ans = logical
1
Now, if the value parameter you pass to to struct() is a cell array, then you are considered to be defining a non-scalar struct, and the output struct will have the same size as the cell array. In my example
Sis = struct('dailyprof', {19, [3 11], -5})
Sis = 1×3 struct array with fields:
dailyprof
the cell array is 1 x 3, so you get out a 1 x 3 struct array. The first cell entry will be assigned to the first struct entry, the second cell entry will be assigned to the second struct entry, and so on. So that call to struct is equivalent to
Sis2 = [struct('dailyprof', 19), struct('dailyprof', [3 11]), struct('dailyprof', -5)]
Sis2 = 1×3 struct array with fields:
dailyprof
%or
Sis3 = struct('dailyprof', 19); Sis3(2).dailyprof = [3 11]; Sis3(3).dailyprof = -5;
isequal(Sis, Sis2)
ans = logical
1
isequal(Sis, Sis3)
ans = logical
1
Again, the [3 11] here is just standard list-building syntax, equivalent to horzcat(3, 11) creating a 1 x 2 vector whose first element is 3 and whose second element is 11.
The reason I used the example,
Sis = struct('dailyprof', {19, [3 11], -5})
with different numbers of values for each field entries was to illustrate that using
[Sis.dailyprof_Orig] = Sis.dailyprof;
transfers each corresponding field, rather than transfering "first value to the first output, second value to the second output" and so on.
Your attempt with
vb=[Sis.dailyprof];
with the sample contents I showed off 19, [3 11], -5, would have resulted in a single numeric vector, [19 3 11 -5] and then you would not be able to tell which values to transfer to which struct entry. For this kind of work, do not use [] to bundle together the values to be transfered.
Now, when you use
[Sis.dailyprof_Orig]
on the left-hand side of the =, then that is a syntax that uses "struct expansion". In the example case where Sis is a 1 x 3 struct, your code
[Sis.dailyprof_Orig]=deal(vb);
would be treated as-if you had written
[Sis(1).dailyprof_Orig, Sis(2).dailyprof_Orig, Sis(3).dailyprof_Orig] = deal(vb);
and that would fail because the vector vb does not have at least 3 output parameters.
However
[Sis.dailyprof_Orig] = Sis.dailyprof;
does an implied deal() and does struct expansion on the right hand side as well, so that line is treated like
[Sis(1).dailyprof_Orig, Sis(2).dailyprof_Orig, Sis(3).dailyprof_Orig] = deal(Sis(1).dailyprof, Sis(2).dailyprof, Sis(3).dailyprof);
piero
piero 2023 年 11 月 8 日
thank you for explenation

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

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 11 月 8 日
編集済み: Sulaymon Eshkabilov 2023 年 11 月 8 日
Understood your question correctly and without seeing your data. These commands work ok, e.g.:
A = randi(13, 7, 1);
DD.Set1 = A;
% Start the simulation :)
B = DD.Set1;
DD.Set2 = deal(B)
DD = struct with fields:
Set1: [7×1 double] Set2: [7×1 double]
[O1, O2] = deal(DD.Set1, DD.Set2)
O1 = 7×1
6 1 6 2 8 2 7
O2 = 7×1
6 1 6 2 8 2 7
That would be helpful to see your sample data.

カテゴリ

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