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 operationsproperties myCell % Cell array to be operated uponendmethods% Constructor to initialize the cell arrayfunctionobj = CellOperations(cellInput) assert(iscell(cellInput), 'Input should be a cell array');
obj.myCell = cellInput;
end% Add element(s) at the end of cell arrayfunctionadd(obj, elements)if ~iscell(elements)
elements = {elements};
end obj.myCell = [obj.myCell elements];
end% Find the first element in cell arrayfunctionfirst = findFirst(obj) first = obj.myCell{1};
end% Find the last element in cell arrayfunctionlast = findLast(obj) last = obj.myCell{end};
end% Remove the first element from cell arrayfunctionremoveFirst(obj) obj.myCell(1) = [];
endendend
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'