How can I create a method which takes an excel file as an argument in a constructor?

2 views (last 30 days)
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);
  2 Comments
Cedric
Cedric on 15 Jan 2013
Edited: Cedric on 15 Jan 2013
Which version of MATLAB are you using? After adding all missing end, your code is working on mine (R2011b).
Jordan Ledvina
Jordan Ledvina on 15 Jan 2013
hey Cedric, thanks for the prompt reply. I'm using R2012b.
Did you run it with an input like : BankAccount('sample.xlsx',55)
where sample.xlsx is a file in your matlab folder?
I guess I'm not sure why VEC needs to be a public field? What does that error typically signify? Even when I try to make it public, matlab doesn't like it.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 15 Jan 2013
Edited: Cedric on 15 Jan 2013
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

More Answers (1)

Jordan Ledvina
Jordan Ledvina on 16 Jan 2013
Hey Cedric,
It works. I commented out handle then I restored it and it worked just fine.
Not really sure what the problem was but I thank you all the same for your expedient and thorough answer.
  1 Comment
Cedric
Cedric on 16 Jan 2013
Great! I already had issues with '<' in other contexts after cut and paste from PDFs. If you deleted < handle and rewrote it afterwards it might be something similar that happened.. I guess you'll discover that after your next cut and paste ;-)
Cheers,
Cedric

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!