object oriented program that can add element to cell, find the first element in a cell, find the last element in a cell, removes the first element in a cell in matlab

Here is an example implementation of an object-oriented program in MATLAB to perform the actions mentioned:

main.m
classdef CellOperations
    %CELLOPERATIONS: Class definition for cell array operations
    
    properties
        myCell % Cell array to be operated upon
    end
    
    methods
        % Constructor to initialize the cell array
        function obj = CellOperations(cellInput)
            assert(iscell(cellInput), 'Input should be a cell array');
            obj.myCell = cellInput;
        end
        
        % Add element(s) at the end of cell array
        function add(obj, elements)
            if ~iscell(elements)
                elements = {elements};
            end
            obj.myCell = [obj.myCell elements];
        end
        
        % Find the first element in cell array
        function first = findFirst(obj)
            first = obj.myCell{1};
        end
        
        % Find the last element in cell array
        function last = findLast(obj)
            last = obj.myCell{end};
        end
        
        % Remove the first element from cell array
        function removeFirst(obj)
            obj.myCell(1) = [];
        end
    end
end
1077 chars
39 lines

Example usage:

main.m
>> c = CellOperations({'apple', 'banana'});
>> c.add({'cherry', 'date'})
>> c.myCell

ans =

  1×4 cell array

    'apple'    'banana'    'cherry'    'date'

>> c.findFirst()

ans =

apple

>> c.findLast()

ans =

date

>> c.removeFirst()
>> c.myCell

ans =

  1×3 cell array

    'banana'    'cherry'    'date'
312 chars
31 lines

gistlibby LogSnag