view Cell.m @ 577:e45c9b56d50d feature/grids

Add an Empty grid class The need turned up for the flexural code when we may or may not have a grid for the open water and want to plot that solution. In case there is no open water we need an empty grid to plot the empty gridfunction against to avoid errors.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 07 Sep 2017 09:16:12 +0200
parents 025f084187d1
children 2ce903f28193
line wrap: on
line source

% Cell is a reimplementation of matlabs cell array with the benefit that it is subclassable
% It might be used for giving a typename to a cellarray to increase readability of the code.
classdef Cell
    properties
        data
    end

    methods
        function obj = Cell(data)
            default_arg('data', {});
            if ~iscell(data)
                error('Input argument to Cell must be a cell array');
            end

            obj.data = data;
        end

        function str = toString(obj)
            str = sprintf('%s%s', class(obj), toString(obj.data));
        end

        function s = size(A)
            s = size(A.data);
        end

        function l = length(A)
            l = length(A.data);
        end

        function ind = end(A,k,n)
            ind = builtin('end',A.data, k, n);
        end

        function B = transpose(A)
            b = A.data.';
            B = callConstructor(A, b);
        end

        function B = ctranspose(A)
            b = A.data';
            B = callConstructor(A, b);
        end

        function A = subsasgn(A, S, B)
            a = subsasgn(A.data, S, B);
            A = callConstructor(A, a);
        end

        function B = subsref(A, S)
            switch S(1).type
                case '()'
                    b = subsref(A.data, S(1));
                    B = callConstructor(A, b);
                    if length(S) > 1
                        B = subsref(B,S(2:end));
                    end
                case '{}'
                    B = subsref(A.data, S);
                case '.'
                    B = builtin('subsref',A, S);
                otherwise
                    error('unreachable');
            end
        end

        function C = horzcat(varargin)
            dataArray = cell(1, length(varargin));

            for i = 1:length(varargin)
                dataArray{i} = varargin{i}.data;
            end

            c = horzcat(dataArray{:});
            C = callConstructor(varargin{1}, c);
        end

        function C = vertcat(varargin)
            dataArray = cell(1, length(varargin));

            for i = 1:length(varargin)
                dataArray{i} = varargin{i}.data;
            end

            c = vertcat(dataArray{:});
            C = callConstructor(varargin{1}, c);
        end
    end
end