【问题标题】:Matlab: Cell contents assignment to a non-cell array objectMatlab:单元格内容分配给非单元格数组对象
【发布时间】:2016-12-01 08:03:16
【问题描述】:

我在执行多个循环时遇到了上述错误。我真的不知道如何解释这个问题,但我会尽力而为

代码:

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

【问题讨论】:

  • 错误是说你试图将单元格的内容分配到一个数组中,比如 A = Cell(1,:) ;你可以试试std(cell2double(filt_sig-this.segments{i}(k).ref)) 看看会发生什么。
  • @GameOfThrows,我没有找到任何叫 cell2double 的东西。
  • @CaptainFuture,我进行了调试,我在该行停止了程序并计算了命令窗口上的值。在这两个实例上,它都会返回一个值。
  • 我没有在代码中实现它,而是再次停止了程序并在命令窗口@fc中检查了'iscell(std(filt_sig-this.segments{i}(k).ref))' = 1,fc = 2,都返回'0',我真的认为问题出在左侧。
  • @user5603723 是的,对不起,我的意思是 str2double - 在办公室度过漫长的一天。

标签: arrays matlab for-loop cell-array


【解决方案1】:

问题似乎在于您希望如何动态访问主结构的成员。您将 main 声明为结构,使用

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 

【讨论】: