Turn a structure into an array
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Jan
on 1 Dec 2011
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);
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!