How can I make a structure array

47 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2023-1-23
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
Name Size Bytes Class Attributes datastruct 1x5 1968 struct
datastruct
datastruct = 1×5 struct array with fields:
Spectra freqs
datastruct(1).Spectra
ans = 10×1
0.7937 -0.6929 0.6557 -0.1090 -0.5371 -0.1283 -0.0640 -0.9109 1.6227 -1.6004
spectras{1}
ans = 10×1
0.7937 -0.6929 0.6557 -0.1090 -0.5371 -0.1283 -0.0640 -0.9109 1.6227 -1.6004
  4 个评论
Neda Deljavan
Neda Deljavan 2023-1-25
Thanks a billion for your response.
I want to have "Spectra" and "freqs" like below.
Walter Roberson
Walter Roberson 2023-1-26
t1 = cellfun(@squeeze, num2cell(spectra, 2), 'uniform', 0);
t2 = num2cell(freqs);
output = struct('spectra', t1(:) , 'freqs', t2(:) );

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by