Matlab:单元格内容分配给非单元格数组对象
Matlab: Cell contents assignment to a non-cell array object
我在经历多个循环时遇到上述错误。我真的不知道怎么解释这个问题,但我会尽力
代码:
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 =1
,k=1
,a = 2
问题似乎出在您想动态访问主结构成员的方式上。您将 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
我在经历多个循环时遇到上述错误。我真的不知道怎么解释这个问题,但我会尽力
代码:
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 =1
,k=1
,a = 2
问题似乎出在您想动态访问主结构成员的方式上。您将 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