为什么结构字段变空?

时间:2012-03-08 15:01:50

标签: matlab

当我访问结构字段时,如s(1)。'fieldname',它将返回空白状态。只能看到最后一个值,即s(97)。'fieldname'。这是我的代码:

clc;
clear all;
data1= load('mydata.txt');
a=1;n=1;count=1;m=0;
while (count<98)                           % main loop starts
    for i=a:a+1023
        mydata1(i-m*1024)=data1(i);        % taking 1k points in mydata1
    end
    m=m+1;
    newdata=10*log10(abs(fft(mydata1)));   % taking fft in newdata from mydata
    for j=1:512
        newdata1(2*j-1)=newdata(j);        % interpolation of newdata to newdata1
        newdata1(2*j)=newdata(j);          % newdata1 size is 1024
    end
    a=j*n+513;
    n=2*count+1;
%     plot(newdata1);
%     pause(1);                            % setting threshold
    th=18;
    newdata2(newdata1<th)=0;               % newdata2 has 1024 0s and many 1s 
    newdata2(newdata1>=th)=1;
    num=0;
    for k=1:1023                           % loop for reducing many 1s
        if((newdata2(k)==1)&&(newdata2(k+1)==1))
        num=num+1;
        end
        if(num>0 && (newdata2(k+1)==0 || k==1023))
            for p = k-num:k
                newdata2(p)=0;
            end
            p= (k-num)+floor(num/2);
            newdata2(p)=1;
            num=0;
        end                                % loop ends
   end
   newdata2(1024)=0;                       % newdata2 with only 1s and 0s
   binnum = find(newdata2);
   x=length(binnum);
   frequency=((100000/1024)*binnum);
   for y=1:x
       strength(y)=(newdata1(binnum(y)));
   end
   s= struct;                              % writing into a structure
   s(count).frame=count;
   s(count).freq=frequency;
   s(count).str=strength;
   count=count+1;
end  

我哪里错了?

1 个答案:

答案 0 :(得分:0)

s= struct; %writing into a structure行在while循环的每次迭代中创建一个新的结构变量。将此行移出循环,或完全省略。