Matlab:将单元格内容分配给非单元格数组对象

时间:2016-07-27 10:46:24

标签: arrays matlab for-loop cell-array

我在经历多个循环时遇到上述错误。我真的不知道如何解释这个问题,但我会尽我所能

代码:

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,第二个循环用于信号,第三个循环用于信号的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

1 个答案:

答案 0 :(得分:1)

问题似乎在于您希望如何动态访问主结构的成员。您使用

将main声明为struct
main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{});

但是使用大括号{}无法访问结构成员。这是与先前关于struct数组的动态索引的讨论的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