我試圖添加一個結構作爲container.Map
的值,例如我試圖將segments
strcuture添加到cont
容器。使用結構作爲容器的值。地圖
代碼:
classdef datHandle < handle
properties
cont = containers.Map('KeyType','int32','ValueType','any');
end
methods
function this = addsignal(this, varargin)
interval = diff(varargin{2});
[~, locations] = findpeaks(interval,'THRESHOLD',0.7);
edges = [0; locations; numel(varargin{1})+1]; %note that 0 and one past the end is on purpose
newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1));
%this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
for edgeidx = 1 : numel(edges) - 1
newsegments(edgeidx).signal = varargin{1}((edges(edgeidx)+1 : edges(edgeidx+1)-1));
newsegments(edgeidx).time = varargin{2}(edges(edgeidx)+1 : edges(edgeidx+1)-1);
end
global p
if length(this.map) == 0
p = 2;
this.cont(1) = newsegments;
else
this.cont(p) = newsegments;
p = p+1;
end
end
end
end
這甚至可能,它增加了罰款,但是當我嘗試檢索值。
cont.values(1)
我越來越:
Error using containers.Map/values Parameter must be 'cell'.
EDIT1:
matlab Script file:
filename1 = import('file1')
singal1 = filename1.yaxis
time1 = filename1.xaxis
filename2 = import('file2')
signal2 = filename2.yaxis
time2 = filename2.yaxis
f = ltiFilter.dathandle();
f.addsignal(signal1,time1)
f.addsignal(signal2,time2)
首先創建一個[最小工作示例](http://stackoverflow.com/help/mcve)。該代碼是無效的Matlab代碼。其次,你定義'cont',但是在你使用'this.map'的函數中。最後,'cont.values'返回一個單元格數組。使用'cont(1)'獲取存儲的值。 –
謝謝你,我只是編輯了代碼。所以當我做'cont(1)'時,它返回一個帶有3個字段的'3x1'結構數組,有沒有什麼時候我可以添加一個字段到這個結構而不創建更多的結構? – user5603723
不完全確定你想要達到什麼。在上面的代碼中,'this.segments'每次都指向同一個結構體。你想爲每個分段或只是一個數組結構不同的結構?也許你不需要地圖? –