Matlab:单元格内容分配给非单元阵列对象
问题描述:
我在经历多个循环时遇到了上述错误。我真的不知道该怎么解释这个问题,但我会尽我所能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 =1
,k=1
,a = 2
答
问题似乎与您想如何动态访问主结构的成员一致。你声明为结构主体,
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
错误是说你是试图将单元格的内容分配到数组中,如A = Cell(1,:);你可以试试'std(cell2double(filt_sig-this.segments {i}(k).ref))'看看会发生什么。 – GameOfThrows
@GameOfThrows,我没有找到任何叫做cell2double的东西。 – user5603723
@CaptainFuture,我做了调试,我停止了该行的程序并在命令窗口中调用了该值。在两个实例上它都会返回一个值。 – user5603723