Turn a structure into an array

조회 수: 1 (최근 30일)
Sara
Sara 2011년 12월 1일
Hello everybody,
i'm starting with matlab and i have a problem with this code:
ev = load('EV.mat'); %potenciales evocados
fs = 3200; %sampling rate = 3200 Hz
slen = length(ev);
t=[1:slen]/fs;
vent=256; %amplitud ventana
sum=zeros(1,vent); %vector de ceros con 256 elementos
n=100; %numero de realizaciones
for i=0:vent-1
for k=1:25600
sum(i+1)=sum(i+1)+ev(i+k);
k=k+256;
end
end
i should sum 100 windows, each one containing 256 samples, of the same signal, the signal ev.Tthe problem is that the program gives me the following errors:
??? Undefined function or variable 'ev'.
??? Undefined function or method 'plus' for input arguments of type 'struct'.
Error in ==> Potenciales_Evocados at 17 sum(i+1)=sum(i+1)+ev(i+k);
Then, i guess i should transform this structure into an array, but i don't know how..! I've tried with "c = struct2cell(s)" but i create a cell array and the error is the same ??? Undefined function or method 'plus' for input arguments of type 'cell'.
In the command window i have this:
ev =
ev: [727040x1 double]
>> length(ev)
ans =
1
Please can anyone help me? Thank you very much in advance

채택된 답변

Jan
Jan 2011년 12월 1일
It seems like after:
ev = load('EV.mat')
"ev" is a struct, which contains the field "ev". So try this:
Data = load('EV.mat')
ev = Data.ev;
BTW. using "sum" as name of a variable shadows the built-in function with the same name. It is recommend to avoid such shadowing, because it leads to unexpected results frequently.
A faster method to sum the chunks without loops:
S = sum(reshape(ev, 256, []), 2);
  댓글 수: 1
Sara
Sara 2011년 12월 1일
Perfect!! Thanks a lot!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by