2016-07-26 25 views
0

我試圖添加一個結構作爲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)

+0

首先創建一個[最小工作示例](http://stackoverflow.com/help/mcve)。該代碼是無效的Matlab代碼。其次,你定義'cont',但是在你使用'this.map'的函數中。最後,'cont.values'返回一個單元格數組。使用'cont(1)'獲取存儲的值。 –

+0

謝謝你,我只是編輯了代碼。所以當我做'cont(1)'時,它返回一個帶有3個字段的'3x1'結構數組,有沒有什麼時候我可以添加一個字段到這個結構而不創建更多的結構? – user5603723

+0

不完全確定你想要達到什麼。在上面的代碼中,'this.segments'每次都指向同一個結構體。你想爲每個分段或只是一個數組結構不同的結構?也許你不需要地圖? –

回答

0

這裏是一個可能的解決方案

classdef datHandle < handle 
    properties 
     segments = {}; 
    end 

    methods 
     function this = addsignal(this, varargin) 

      interval = diff(varargin{2}); 

      [~, locations] = findpeaks(interval,'THRESHOLD',0.7); 

      %note that 0 and one past the end is on purpose 
      edges = [0; locations; numel(varargin{1})+1]; 
      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 

     this.segments{end+1} = newsegments; 
     end 
    end 
end 

下面是它如何工作的一個例子。段被存儲在單元陣列中。

>> d = datHandle; 
>> d.addsignal([0 0 0 0 4 0 0 0], [0 0 0 0 4 0 0 0]) 

ans = 

    datHandle with properties: 

    segments: {[2x1 struct]} 

>> d.addsignal([0 0 0 0 4 0 0 0], [0 0 0 0 4 0 0 0]) 

ans = 

    datHandle with properties: 

    segments: {[2x1 struct] [2x1 struct]} 

>> d.segments{1} 

ans = 

2x1 struct array with fields: 

    signal 
    time 

>> d.segments{2}(1) 

ans = 

    signal: [0 0 0] 
     time: [0 0 0] 
+0

這就是我一直在尋找的!如果我想添加一個額外的字段。我應該做'd.segments {2}(1).newfield = []' – user5603723

+0

是的沒有問題。請注意,額外的字段將被添加到「細分」中的所有結構中 –

+0

非常感謝幫助! – user5603723