How can I make a structure array

22 visualizaciones (últimos 30 días)
Neda Deljavan
Neda Deljavan el 23 de En. de 2023
Comentada: Walter Roberson el 26 de En. de 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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by