フィルターのクリア

Concatenating multiple struct fields into a single matrix or array but maintaining dimensions

26 ビュー (過去 30 日間)
Vyte Jan
Vyte Jan 2024 年 10 月 14 日 20:08
コメント済み: Voss 2024 年 10 月 15 日 1:17
Hi,
I've got a struct question. I have read a lot of similar questions & answers online, but its just not what I am trying to do.
I have struct: s=A.B.C and in location C I have a 100 fields with different string names (ie name01, name02..). Each array is 128x57 of numbers.
I want to concatinate these arrays into one single matrix of 12800 x 57 (maintaining the two dimensions).
-if possible- I'd like to know how to concatinate 'everything' in s=A.B.C location. Although, I would be interested to know how I could do a for loop to pass through a list of string names (name01, name02...) as something more advanced.
I have tried cellfun but it concatinated all the values into 1 dimension. I think I'm missing something very small.
I have added a screenshot of what C location displays.
Thanks!
V

採用された回答

Voss
Voss 2024 年 10 月 14 日 20:31
For demonstration, first I create a similar struct, with 3 fields in A.B.C:
N = 3;
args = [compose('name%02d',1:N); permute(num2cell(rand(128,57,N),[1 2]),[1 3 2])];
A = struct('B',struct('C',struct(args{:})));
A.B.C
ans = struct with fields:
name01: [128x57 double] name02: [128x57 double] name03: [128x57 double]
Make a cell array containing the contents of all fields of A.B.C:
tmp = struct2cell(A.B.C)
tmp = 3x1 cell array
{128x57 double} {128x57 double} {128x57 double}
To concatenate the contents of all fields of A.B.C together into a matrix:
result = vertcat(tmp{:})
result = 384×57
0.7016 0.5247 0.4862 0.8103 0.5211 0.8613 0.8348 0.7944 0.3241 0.1879 0.8924 0.2318 0.1042 0.6421 0.8176 0.1636 0.1577 0.1961 0.6061 0.0339 0.3641 0.5759 0.2847 0.1911 0.2540 0.1149 0.6864 0.2224 0.7385 0.3767 0.2621 0.3619 0.7734 0.0089 0.0116 0.5627 0.5977 0.8290 0.0089 0.6186 0.7530 0.1505 0.2660 0.8529 0.7806 0.5531 0.3912 0.0874 0.8604 0.3982 0.0738 0.9015 0.5513 0.8662 0.4306 0.3957 0.7498 0.2443 0.3472 0.2054 0.5630 0.0517 0.8014 0.5588 0.0532 0.7560 0.9364 0.2610 0.0868 0.3247 0.6822 0.5275 0.3303 0.8314 0.1889 0.9540 0.1451 0.9704 0.2671 0.7753 0.4977 0.5302 0.9841 0.7130 0.0383 0.8599 0.6455 0.9030 0.9149 0.2388 0.5687 0.9936 0.0542 0.8198 0.9997 0.2423 0.3567 0.8020 0.9059 0.7126 0.5800 0.6057 0.1721 0.4625 0.0752 0.1642 0.1244 0.2837 0.7200 0.2966 0.9713 0.6430 0.8155 0.3751 0.5341 0.8163 0.2921 0.8052 0.5792 0.9894 0.0475 0.1832 0.5740 0.0422 0.4658 0.4820 0.5843 0.0125 0.6813 0.0260 0.7554 0.1029 0.7144 0.7884 0.0089 0.0718 0.3100 0.2234 0.9659 0.1897 0.6523 0.9967 0.2368 0.2868 0.4206 0.5245 0.2047 0.4164 0.4774 0.1258 0.5088 0.8898 0.1491 0.7946 0.3345 0.3264 0.3214 0.6803 0.9500 0.6012 0.5480 0.4982 0.0083 0.9412 0.8471 0.2508 0.6779 0.2592 0.0837 0.7914 0.2430 0.9371 0.0049 0.7605 0.9514 0.1945 0.3468 0.2530 0.1946 0.0056 0.7659 0.3749 0.0376 0.5007 0.0392 0.4582 0.9460 0.3561 0.9162 0.0650 0.4116 0.5289 0.7467 0.1570 0.0253 0.1596 0.2651 0.3299 0.1725 0.1516 0.4624 0.8052 0.4739 0.5744 0.6836 0.6825 0.2760 0.1197 0.9845 0.5648 0.9507 0.1311 0.8476 0.1509 0.3859 0.1826 0.7287 0.0391 0.4908 0.7467 0.7834 0.7271 0.1525 0.4270 0.5421 0.5615 0.5153 0.3698 0.9105 0.9994 0.5853 0.7787 0.2159 0.0872 0.9116 0.3400 0.3118 0.5427 0.5982 0.5155 0.0619 0.2524 0.2880 0.4094 0.9142 0.3345 0.5506 0.1278 0.3061 0.9158 0.3516 0.5175 0.0004 0.7909 0.1788 0.2399 0.9074 0.6564 0.3081 0.0641 0.9159 0.5541 0.1051 0.8818 0.5873 0.8778 0.7733 0.2257 0.7580 0.8650 0.8074 0.2022 0.5826 0.4097 0.7341 0.2222 0.5092 0.6195 0.5186 0.7212 0.1556 0.7701 0.5459 0.9372 0.2567 0.9235 0.8696 0.6289 0.2800 0.4231 0.0879 0.7028 0.3715 0.3526 0.5253 0.8801 0.1735 0.0905 0.2753 0.0422
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
To concatenate the contents of only certain fields of A.B.C together:
idx = ismember(fieldnames(A.B.C),{'name01','name03'}); % taking name01 and name03 only, for example
result = vertcat(tmp{idx})
result = 256×57
0.7016 0.5247 0.4862 0.8103 0.5211 0.8613 0.8348 0.7944 0.3241 0.1879 0.8924 0.2318 0.1042 0.6421 0.8176 0.1636 0.1577 0.1961 0.6061 0.0339 0.3641 0.5759 0.2847 0.1911 0.2540 0.1149 0.6864 0.2224 0.7385 0.3767 0.2621 0.3619 0.7734 0.0089 0.0116 0.5627 0.5977 0.8290 0.0089 0.6186 0.7530 0.1505 0.2660 0.8529 0.7806 0.5531 0.3912 0.0874 0.8604 0.3982 0.0738 0.9015 0.5513 0.8662 0.4306 0.3957 0.7498 0.2443 0.3472 0.2054 0.5630 0.0517 0.8014 0.5588 0.0532 0.7560 0.9364 0.2610 0.0868 0.3247 0.6822 0.5275 0.3303 0.8314 0.1889 0.9540 0.1451 0.9704 0.2671 0.7753 0.4977 0.5302 0.9841 0.7130 0.0383 0.8599 0.6455 0.9030 0.9149 0.2388 0.5687 0.9936 0.0542 0.8198 0.9997 0.2423 0.3567 0.8020 0.9059 0.7126 0.5800 0.6057 0.1721 0.4625 0.0752 0.1642 0.1244 0.2837 0.7200 0.2966 0.9713 0.6430 0.8155 0.3751 0.5341 0.8163 0.2921 0.8052 0.5792 0.9894 0.0475 0.1832 0.5740 0.0422 0.4658 0.4820 0.5843 0.0125 0.6813 0.0260 0.7554 0.1029 0.7144 0.7884 0.0089 0.0718 0.3100 0.2234 0.9659 0.1897 0.6523 0.9967 0.2368 0.2868 0.4206 0.5245 0.2047 0.4164 0.4774 0.1258 0.5088 0.8898 0.1491 0.7946 0.3345 0.3264 0.3214 0.6803 0.9500 0.6012 0.5480 0.4982 0.0083 0.9412 0.8471 0.2508 0.6779 0.2592 0.0837 0.7914 0.2430 0.9371 0.0049 0.7605 0.9514 0.1945 0.3468 0.2530 0.1946 0.0056 0.7659 0.3749 0.0376 0.5007 0.0392 0.4582 0.9460 0.3561 0.9162 0.0650 0.4116 0.5289 0.7467 0.1570 0.0253 0.1596 0.2651 0.3299 0.1725 0.1516 0.4624 0.8052 0.4739 0.5744 0.6836 0.6825 0.2760 0.1197 0.9845 0.5648 0.9507 0.1311 0.8476 0.1509 0.3859 0.1826 0.7287 0.0391 0.4908 0.7467 0.7834 0.7271 0.1525 0.4270 0.5421 0.5615 0.5153 0.3698 0.9105 0.9994 0.5853 0.7787 0.2159 0.0872 0.9116 0.3400 0.3118 0.5427 0.5982 0.5155 0.0619 0.2524 0.2880 0.4094 0.9142 0.3345 0.5506 0.1278 0.3061 0.9158 0.3516 0.5175 0.0004 0.7909 0.1788 0.2399 0.9074 0.6564 0.3081 0.0641 0.9159 0.5541 0.1051 0.8818 0.5873 0.8778 0.7733 0.2257 0.7580 0.8650 0.8074 0.2022 0.5826 0.4097 0.7341 0.2222 0.5092 0.6195 0.5186 0.7212 0.1556 0.7701 0.5459 0.9372 0.2567 0.9235 0.8696 0.6289 0.2800 0.4231 0.0879 0.7028 0.3715 0.3526 0.5253 0.8801 0.1735 0.0905 0.2753 0.0422
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
Vyte Jan
Vyte Jan 2024 年 10 月 14 日 22:27
Amazing! exactly what I needed:)
Just wondering - for the second part (To concatenate the contents of only certain fields of A.B.C together)
I tried to extract the filenames and substitute it, but that didnt work:
names=fieldnames(A.B.C)
idx = ismember(fieldnames(A.B.C),{names})
The 'names' are 100x1 cell. I also tried converting 'names' into a string but that didn't work. Is it possible to convert a column of strings/characters into a single row and substitute it into the line of code?
Thank you!
V
Voss
Voss 2024 年 10 月 15 日 1:17
You're welcome!
You'd have to specify some condition on which fields you want to use. In the example, I hard-coded name01 and name03, but you can use any condition that generates a logical vector idx the same length as the number of fields.

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

その他の回答 (0 件)

カテゴリ

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