MATLAB: How to create a method which takes an excel file as an argument in a constructor

MATLABmethod argumentsobject-orientedpassing files through arguments

Basically, I'm trying to construct a method that allows users to input a data file. It can be excel or ascii, and the method reads it as a matrix which can then be tested on.
Here is some of the rudimentary code I've modified from the tutorial:
classdef BankAccount < handle
properties (Hidden)
AccountStatus = 'open';
end
properties (SetAccess = private)
VEC
AccountBalance = 0;
end
events
InsufficientFunds
end
methods
function BA = BankAccount(filename, InitialBalance)
BA.VEC = xlsread(filename);
BA.AccountBalance = InitialBalance;
then I try: BankAccount('sample.xlsx',55)
I'm getting an error: No public field VEC exists for class BankAccount.
Error in BankAccount (line 20) BA.VEC = xlsread(filename);

Best Answer

My guess is that it has nothing to do with the fact that you want to read an xls file.
If you really have the code that you gave above, with 3 additional end at the end and nothing else like setters/getters or overloads of subsasgn/subsref, i.e.:
classdef BankAccount < handle
properties (Hidden)
AccountStatus = 'open';
end
properties (SetAccess = private)
VEC
AccountBalance = 0;
end
events
InsufficientFunds
end
methods
function BA = BankAccount(filename, InitialBalance)
BA.VEC = xlsread(filename);
BA.AccountBalance = InitialBalance;
end
end
end
then the error..
  • Could be related to something new in R2012a that I will discover in less than 2 weeks according to my license manager ;-)
  • Could come from a upper/lowercase mismatch between the declaration of properties and the constructor (like BA.VEc=.., that I don't observe in what you pasted actually).
  • I am unsure whether the editor would allow that, but could you have some sort of invisible special character that came during the copy-paste that you probably made from the PDF manual about OOP in MATLAB?
You could perform a few checks actually. Try replacing
BA.VEC = xlsread(filename);
with
BA.VEC = [] ;
so you eliminate anything related to xls stuff. If it still generates an error on VEC, try commenting the line and see it is specific to VEC or if it happens as well with AccountBalance.
You could also comment out the events block and avoid subclassing handle, to see if the issue comes from some conflict with handle methods/properties..
classdef BankAccount %< handle
...
%events
% InsufficientFunds
%end
...
Cheers,
Cedric