使用structure作为containers.Map的值

时间:2016-07-26 05:48:07

标签: matlab dictionary

我正在尝试将结构添加为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:

&#13;
&#13;
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)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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]