How can I make a structure array

조회 수: 22 (최근 30일)
Neda Deljavan
Neda Deljavan 2023년 1월 23일
댓글: Walter Roberson 2023년 1월 26일
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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by