How can I make a structure array
47 views (last 30 days)
Show older comments
Hello everyone,'
I want to make a struct array, or example "Spectra" or "freqs".
How can I implement it on my own (EEG)data?
Thanks in advance for your help.
Neda
0 Comments
Answers (1)
Walter Roberson
on 23 Jan 2023
If you pass struct() a cell array of data for a field, then it creates a struct array with the same size() as the size() of the cell array.
%setup to create cell arrays of data
N = 5;
spectras = arrayfun(@(n) randn(10,1), 1:N, 'uniform', 0);
freqs = arrayfun(@(n) sort(randi([0 999], 10, 1)), 1:N, 'uniform', 0);
%now that we have a cell arrays of data we can convert to struct array:
datastruct = struct('Spectra', spectras, 'freqs', freqs);
%verify that the struct is what we expect
whos datastruct
datastruct
datastruct(1).Spectra
spectras{1}
4 Comments
Walter Roberson
on 26 Jan 2023
t1 = cellfun(@squeeze, num2cell(spectra, 2), 'uniform', 0);
t2 = num2cell(freqs);
output = struct('spectra', t1(:) , 'freqs', t2(:) );
See Also
Categories
Find more on Multidimensional Arrays 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!