How can I create a method which takes an excel file as an argument in a constructor?
2 views (last 30 days)
Show older comments
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
Accepted Answer
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
0 Comments
More Answers (1)
Jordan Ledvina
on 16 Jan 2013
1 Comment
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!