How can I make a structure array

22 vues (au cours des 30 derniers jours)
Neda Deljavan
Neda Deljavan le 23 Jan 2023
Commenté : Walter Roberson le 26 Jan 2023
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

Réponses (1)

Walter Roberson
Walter Roberson le 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
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 commentaires
Neda Deljavan
Neda Deljavan le 25 Jan 2023
Thanks a billion for your response.
I want to have "Spectra" and "freqs" like below.
Walter Roberson
Walter Roberson le 26 Jan 2023
t1 = cellfun(@squeeze, num2cell(spectra, 2), 'uniform', 0);
t2 = num2cell(freqs);
output = struct('spectra', t1(:) , 'freqs', t2(:) );

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by