How do you create a matrix of matrices in a method?

1 view (last 30 days)
I'm trying to design a class whose instance is an array of matrices. The matrices are read from some source file, xls, csv, etc. The super matrix is of size 30, and the user can add files in each slot. The declaration needs to allow for dynamic possibilities. But I'm not sure how to do this syntactically. I'm running MATLAB R2012B.
Here is the code thus far:
classdef BankAccount < handle properties (Hidden) end
properties (SetAccess = private)
VEC
IndicatorCount = 0;
Index = zeros(30); %This obviously won't work because of a dimension mismatch
end
methods
function BA = BankAccount(filename)
if nargin ~=0 %handles empty declarations
BA.VEC = xlsread(filename);
BA.IndicatorCount = FI.IndicatorCount +1; %find shorthand
disp(FI.IndicatorCount)
BA.Index(FI.IndicatorCount)= FI.VEC; %need the Index to be able to take on %different matrix sizes dynamically
end
end
end
end
if someone can tell me how to do this in a smart way I would appreciate the help

Accepted Answer

Cedric
Cedric on 17 Jan 2013
Edited: Cedric on 18 Jan 2013
Leaving aside OOP, we can say the following:
In your code, FI should be BA (the object) and you should replace Index=zeros(30); with Index=cell(30,1); to create a cell array, and then replace
BA.Index(FI.IndicatorCount)= FI.VEC ;
with
BA.Index{BA.IndicatorCount}= BA.VEC ;
Then you should type
>> doc cell
in your command window and read a bit about cells and cell arrays, and maybe also have a look at my answer there: http://www.mathworks.com/matlabcentral/answers/58977-is-used-as-index-array-in-class so you have a better understanding of cell arrays.
Now about OOP
You cannot implement the code that adds elements to BA.Index in the constructor (method BankAccount), because it will be called only once when the class is instantiated (when the object is created). What you should have is a constructor that initializes relevant properties, and additional methods that can be called to perform actions, like adding/updating data/properties. For example:
properties
accountsCounter
accounts
end
methods
function obj = BankAccount( filename ) % Constructor.
obj.accountsCounter = 0 ;
obj.accounts = cell( 30, 1 ) ; % Prealloc for 30 accounts.
if nargin
obj.add( filename )
end
end
function add( obj, filename )
obj.accountsCounter = obj.accountsCounter + 1 ;
obj.accounts{obj.accountsCounter} = xlsread( filename ) ;
end
end
With that you can create an account or not when you create the object:
>> ba = BankAccount() ;
or
>> ba = BankAccount( 'JohnDoe.xlsx' ) ;
And then add accounts using the add() method:
>> ba.add( 'MrSmith.xlsx' ) ;
.. and you can access "manually" properties just to check the functioning:
>> ba
or
>> ba.accounts{1}

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!