store data from different structures into one variable

17 views (last 30 days)
I have a structure with multiple variables and under each variable are cells that have their own structure. I want to go through each one of these cells (85) and grab some specific values that is 320 each and store them into another variables all together to create one large structure (320x85). Is this possible? Should a loop be used? It seems like I can only read the first entry under my desired variable with this code:
for w = 1:numel(matData)
baseAcc = matData(w).results.learn.acc;
data.acc(w) = fullfile(baseAcc);
end
the matData file is a structure that is 1x85 with 4 fields (results being one of them).
  2 Comments
Jan
Jan on 6 Jul 2021
"I have a structure with multiple variables and under each variable are cells that have their own structure."
It helps to understand the question, if you use the standard expressions. You have a struct array (not "structure") with nested fields (not "multiple variabels"). What are the cells now and which are "their own structure/structs"? Which field has the dimension 320?
Why do you use fullfile with a single input? This replies the input as output.
Of course you can join the data to an array using a loop. Maybe:
acc = zeros(320, numel(matData));
for w = 1:numel(matData)
acc(:, w) = matData(w).results.learn.acc;
end
Sam N
Sam N on 6 Jul 2021
Hi, I'm sorry I'm relatively new to matlab (interning and forced to learn quickly). Thanks for the tips!

Sign in to comment.

Accepted Answer

Peter O
Peter O on 6 Jul 2021
Hi,
Yes this is very possible to do. Are are 320 values stored as a vector in the acc field? If so, try using the arrayfun to loop through access each element of the structure array and then use cell2mat to combine them:
Whipping up a quick MWE:
acc = rand(320,3);
matData(1).results.learn.acc = acc(:,1);
matData(2).results.learn.acc = acc(:,2);
matData(3).results.learn.acc = acc(:,3);
allAcc = cell2mat(arrayfun(@(x) x.results.learn.acc ,matData,'UniformOutput',false));
disp(allAcc)
0.3305 0.0071 0.2586 0.9705 0.1881 0.6474 0.7241 0.1155 0.2407 0.1283 0.1433 0.3469 0.4475 0.0842 0.6326 0.7165 0.1520 0.1345 0.8030 0.8652 0.3265 0.9047 0.2291 0.7381 0.5593 0.3101 0.0406 0.8226 0.1151 0.8703 0.4456 0.9189 0.7150 0.2457 0.3719 0.4739 0.2660 0.3731 0.1966 0.0510 0.3261 0.3153 0.8074 0.9859 0.5915 0.4763 0.2702 0.9230 0.0256 0.4043 0.0472 0.9045 0.0588 0.8741 0.6002 0.0164 0.4950 0.4166 0.1104 0.0410 0.1548 0.9711 0.4050 0.9573 0.3789 0.9214 0.2677 0.9111 0.7192 0.6653 0.8032 0.6870 0.0569 0.0889 0.4955 0.5330 0.0655 0.2201 0.5524 0.1865 0.1615 0.8109 0.5960 0.6208 0.9426 0.4646 0.6145 0.3147 0.2383 0.7157 0.3730 0.0122 0.3307 0.8382 0.2296 0.1103 0.2429 0.6144 0.2215 0.8162 0.6787 0.6565 0.9533 0.2010 0.3777 0.4997 0.8567 0.6851 0.9447 0.7975 0.4815 0.2961 0.0275 0.7623 0.4268 0.8906 0.9356 0.1921 0.2451 0.2726 0.0464 0.0935 0.2142 0.4293 0.7668 0.8360 0.8921 0.3397 0.4625 0.8480 0.8163 0.5649 0.1876 0.7979 0.6095 0.9179 0.8383 0.8810 0.7191 0.6583 0.7228 0.4327 0.6766 0.5285 0.1771 0.9508 0.7382 0.9237 0.1097 0.4895 0.9572 0.8376 0.4543 0.8205 0.8971 0.2492 0.6080 0.6196 0.7474 0.4919 0.3744 0.7150 0.5575 0.9100 0.0471 0.9504 0.2090 0.1917 0.5359 0.6443 0.6268 0.8320 0.5772 0.7284 0.6258 0.3694 0.8145 0.9153 0.0585 0.0828 0.5956 0.5344 0.2964 0.3085 0.2901 0.5143 0.2299 0.7746 0.9961 0.2195 0.3614 0.8543 0.6394 0.8272 0.5765 0.6816 0.3599 0.8498 0.1362 0.8693 0.3822 0.8098 0.2897 0.0863 0.1476 0.2945 0.8820 0.8756 0.8663 0.1426 0.6167 0.1730 0.6306 0.9702 0.7685 0.2988 0.7151 0.3873 0.3143 0.0593 0.8090 0.9572 0.1595 0.9512 0.2018 0.4507 0.6242 0.6306 0.9388 0.9896 0.9853 0.9578 0.1098 0.2299 0.1308 0.7161 0.6047 0.7595 0.8618 0.1850 0.8179 0.1341 0.3541 0.4583 0.8204 0.7924 0.3315 0.5785 0.9852 0.0449 0.7584 0.2069 0.4389 0.2205 0.5582 0.3876 0.2929 0.4620 0.3495 0.4991 0.5202 0.4769 0.3780 0.7994 0.5271 0.9346 0.4386 0.6230 0.7940 0.8368 0.0461 0.7192 0.0148 0.6825 0.5059 0.9985 0.4276 0.4234 0.3613 0.1011 0.5270 0.3150 0.4351 0.9097 0.0921 0.0599 0.0757 0.0969 0.6730 0.7469 0.5428 0.6965 0.1310 0.3180 0.5680 0.2197 0.3288 0.5637 0.2436 0.4496 0.4391 0.4087 0.6577 0.4722 0.6967 0.0948 0.1980 0.6597 0.7463 0.4847 0.1965 0.7565 0.6137 0.8727 0.0871 0.1416 0.1123 0.0584 0.0544 0.8128 0.7879 0.8767 0.8927 0.2032 0.8919 0.0783 0.6946 0.9326 0.6000 0.0277 0.9761 0.9285 0.9387 0.2551 0.8319 0.0808 0.3375 0.0024 0.9799 0.7827 0.7334 0.2815 0.1416 0.3654 0.7729 0.3630 0.4972 0.8302 0.4952 0.3961 0.1291 0.5646 0.6672 0.7641 0.2939 0.2409 0.5622 0.0176 0.7780 0.9370 0.3036 0.7140 0.4955 0.6545 0.7016 0.2556 0.2449 0.4536 0.1146 0.8336 0.6638 0.3829 0.3508 0.1878 0.2272 0.6558 0.7729 0.5645 0.3849 0.4424 0.4592 0.6588 0.8179 0.1402 0.0345 0.4801 0.7354 0.0052 0.7968 0.9874 0.5133 0.7411 0.8859 0.0809 0.6504 0.3326 0.7020 0.3357 0.3472 0.1165 0.5525 0.9046 0.9038 0.5869 0.0631 0.6840 0.9723 0.9104 0.8887 0.9748 0.3601 0.0705 0.2297 0.6303 0.2124 0.7301 0.5187 0.7745 0.0308 0.8004 0.0607 0.0800 0.5352 0.1343 0.6188 0.3213 0.3407 0.2151 0.3641 0.6144 0.3961 0.5190 0.5614 0.5913 0.0037 0.0930 0.8778 0.0122 0.6531 0.1844 0.3203 0.1136 0.1559 0.3155 0.4466 0.4710 0.6826 0.5972 0.3211 0.5528 0.0115 0.7679 0.5824 0.7391 0.3949 0.9627 0.5846 0.9858 0.0582 0.7878 0.3631 0.9898 0.7120 0.0800 0.7273 0.1107 0.9417 0.7668 0.3256 0.2314 0.0666 0.1895 0.4920 0.1530 0.3142 0.3537 0.4449 0.8996 0.9575 0.2106 0.5046 0.6707 0.7970 0.9666 0.9032 0.8888 0.5163 0.2456 0.7744 0.2165 0.1571 0.8706 0.4038 0.9563 0.8232 0.9250 0.2597 0.3939 0.4103 0.0385 0.5294 0.0955 0.5882 0.3935 0.9529 0.6574 0.9723 0.8806 0.5482 0.3747 0.4427 0.2306 0.6680 0.5050 0.0766 0.9845 0.1855 0.2612 0.0846 0.0336 0.4179 0.3744 0.1261 0.1605 0.7515 0.2993 0.1714 0.6689 0.8199 0.8456 0.5828 0.4324 0.3714 0.3527 0.1411 0.4762 0.6845 0.9935 0.0392 0.6038 0.1848 0.9353 0.2593 0.8237 0.8437 0.0885 0.9919 0.1144 0.8288 0.2931 0.3772 0.3544 0.3469 0.1502 0.3244 0.5835 0.2097 0.3187 0.9787 0.1862 0.4853 0.6074 0.4698 0.1096 0.1060 0.5912 0.9882 0.1645 0.1462 0.2913 0.9047 0.2223 0.1653 0.7836 0.8941 0.5168 0.7557 0.2653 0.4388 0.0495 0.4364 0.9390 0.0856 0.7714 0.7234 0.8418 0.4859 0.1365 0.4199 0.1994 0.0275 0.2599 0.0519 0.1491 0.3181 0.9728 0.6074 0.5428 0.3428 0.3221 0.9547 0.6950 0.5724 0.0560 0.0950 0.9371 0.2902 0.7987 0.0479 0.5705 0.3204 0.7211 0.8965 0.1583 0.4730 0.6673 0.0821 0.0872 0.8748 0.7121 0.7737 0.9721 0.0362 0.7596 0.5226 0.2064 0.2745 0.6474 0.2623 0.2265 0.9861 0.0521 0.3188 0.7868 0.1939 0.3126 0.9592 0.9604 0.3399 0.2918 0.5088 0.9271 0.2706 0.2161 0.0489 0.2607 0.7790 0.5057 0.2431 0.3194 0.4845 0.4505 0.1549 0.0426 0.3368 0.5537 0.2193 0.5636 0.5075 0.3726 0.8125 0.1382 0.7537 0.0829 0.8237 0.3877 0.8589 0.1834 0.7471 0.1026 0.2785 0.7941 0.8508 0.0954 0.0413 0.7648 0.4942 0.6531 0.1067 0.9417 0.3467 0.1447 0.9830 0.8546 0.1576 0.5431 0.0025 0.5558 0.1548 0.3163 0.7575 0.7614 0.4752 0.2289 0.3598 0.9213 0.0282 0.9860 0.7732 0.0865 0.8543 0.5397 0.3250 0.9647 0.5929 0.9366 0.9235 0.6521 0.7290 0.9273 0.1686 0.6663 0.9162 0.2262 0.6859 0.3732 0.7193 0.2316 0.4146 0.4276 0.0786 0.7295 0.0949 0.0946 0.1080 0.2586 0.4074 0.4044 0.9001 0.3478 0.3188 0.5155 0.0613 0.2401 0.0766 0.9530 0.5642 0.8571 0.0024 0.8032 0.1189 0.5789 0.5861 0.9715 0.4865 0.4565 0.8007 0.5519 0.6554 0.5621 0.0942 0.6758 0.6220 0.2654 0.5826 0.3179 0.5915 0.1664 0.1595 0.0068 0.1610 0.4393 0.9780 0.5928 0.3895 0.4039 0.3369 0.5028 0.3800 0.4380 0.0674 0.6179 0.7606 0.7488 0.6705 0.1408 0.4041 0.4994 0.1875 0.2015 0.1927 0.5313 0.4806 0.5752 0.7829 0.6605 0.2004 0.4184 0.6701 0.4465 0.3266 0.9564 0.6922 0.2016 0.0789 0.8394 0.8813 0.3160 0.1561 0.8512 0.6464 0.5172 0.1495 0.3078 0.6714 0.8751 0.5568 0.7569 0.3010 0.1759 0.7412 0.7926 0.9329 0.5419 0.5330 0.0272 0.4301 0.4510 0.3070 0.4781 0.7471 0.7902 0.7483 0.9691 0.1220 0.4912 0.2279 0.0959 0.4221 0.8010 0.7099 0.5303 0.2240 0.2844 0.3470 0.8343 0.5416 0.5988 0.6650 0.0119 0.8734 0.6301 0.7302 0.4524 0.6517 0.2513 0.7105 0.8904 0.6195 0.7399 0.1077 0.9043 0.8719 0.8301 0.0389 0.7427 0.1103 0.7262 0.7050 0.5277 0.7658 0.4241 0.7518 0.8603 0.2789 0.2768 0.7275 0.7685 0.8656 0.2736 0.0367 0.8863 0.6940 0.7557 0.8914 0.6718 0.7451 0.9911 0.7157 0.1187 0.4732 0.6941 0.3845 0.5785 0.3242 0.0285 0.5845 0.8284 0.0382 0.2173 0.5670 0.4544 0.4556 0.7500 0.5544 0.9102 0.4659 0.1785 0.0560 0.0818 0.4283 0.2185 0.4014 0.8267 0.4529 0.2825 0.9223 0.9709 0.9037 0.4782 0.2784 0.8076 0.1290 0.0755 0.8406 0.8669 0.2255 0.3162 0.9071 0.2156 0.5722 0.7886 0.5909 0.3335 0.4265 0.2488 0.9220 0.3694 0.1380 0.4431 0.2686 0.1240 0.9028 0.6129 0.9804 0.2714 0.6751 0.2643 0.8520 0.4291 0.5995 0.1972 0.9756 0.5087 0.3577 0.5546 0.7400 0.3714 0.4860 0.6720 0.1943 0.0496 0.8350 0.9609 0.0898 0.6355 0.5955 0.4398 0.3722 0.0678
  4 Comments
Stephen23
Stephen23 on 7 Jul 2021
"In general, MATLAB is more effective working with structs of arrays rather than arrays of structs..."
This rather depends on the nature of the data and the nature of the data processing. I often use structure arrays for storing data that requires the several different values/arrays to be store together with corresponding meta-data: file processing is perhaps the classic example of this (simply using the DIR output).
Structure arrays can be used to generate comma-separated lists, which is a very important and useful syntax:
Peter O
Peter O on 7 Jul 2021
Thanks for pointing those out, @Stephen Cobeldick. Those are very important, effective uses, and I use them all the time for metadata handling too. I was typing quickly and only thinking of the case where you have a lot of common, fragmented data -- a dataset having thousands of scalars that could be arrays. In that case, there's a sizeable amount of memory overhead. (This post between Jan and Doug Hull mentions the problem and the "Ancient FAQ" I wanted to reference but can't locate). On a modern machine and with 99% of use cases I suppose the overhead is not really a dealbreaker for that, either.

Sign in to comment.

More Answers (0)

Categories

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!