2016-07-27 131 views
0

我在經歷多個循環時遇到了上述錯誤。我真的不知道該怎麼解釋這個問題,但我會盡我所能Matlab:單元格內容分配給非單元陣列對象

代碼:

function this = tempaddfilt(this,varargin) 
 
      fc = linspace(1,200,(200/0.5)); 
 
      main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{}); 
 
      for a = 1:length(fc) % fc 
 
       q = 0; 
 
       w = 0 
 
       for i = 1:length(this.segments) % total number signal 
 
        for k = 1:length(this.segments{i}) % total number of segments 
 
         filt_sig = eval(this.segments{i}(k).signal,this.segments{i}(k).signal(1)); % apply filter to the ith singal and kth segemnt 
 
         filt_sig = filt_sig'; 
 
         main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref); % calculate the standard divitation of the filtered signal and previously calculated signal. 
 
         q = q+main{i}(k).seg_err(a); add all the error of the segments for the same FC 
 
         
 
        end 
 
        
 
        main{i}(1).sig_err(a) = q; % assign the sum of all error of the all segemnts of the same signal 
 
        w = w+main{i}(1).sig_err(a); % add all the error of the signals 
 
       end 
 
       main.filt_err = w; % assign the sum of all error of the all signals 
 
      end 
 
      this.error_norm = [this.error_norm ;main]; 
 
     end 
 
     
 
    end

基本上我有3個迴路,第一環是FC,第2循環用於信號,第三循環用於信號的segemnts。程序工作正常時,FC = 1

但是當fc是2,我得到以下錯誤:

Cell contents assignment to a non-cell array object. 

在該行:

main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref); 

那就是當i =1k=1a = 2

+0

錯誤是說你是試圖將單元格的內容分配到數組中,如A = Cell(1,:);你可以試試'std(cell2double(filt_sig-this.segments {i}(k).ref))'看看會發生什麼。 – GameOfThrows

+0

@GameOfThrows,我沒有找到任何叫做cell2double的東西。 – user5603723

+0

@CaptainFuture,我做了調試,我停止了該行的程序並在命令窗口中調用了該值。在兩個實例上它都會返回一個值。 – user5603723

回答

1

問題似乎與您想如何動態訪問主結構的成員一致。你聲明爲結構主體,

main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{}); 

但是使用大括號{}無法訪問結構成員。這裏有一個reference到以前關於結構數組動態索引的討論。所以,基本上,問題在於「main {i}」,這不是動態索引結構成員的有效方法。

請嘗試以下操作。 結構聲明更改爲explanation

main = struct('seg_err',[],'sig_err',[],'filt_err',[],'fc',[]); 

然後,通過

FieldNames = fieldnames(main); 

提取字段名。然後,你可以參考結構成員象

for 
loopIndex = 1:numel(FieldNames) 
main.(FieldNames{loopIndex})(1).seg_err(1) = 1; 
end