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

조회 수: 2 (최근 30일)
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
Cedric
Cedric 2013년 1월 15일
편집: Cedric 2013년 1월 15일
Which version of MATLAB are you using? After adding all missing end, your code is working on mine (R2011b).
Jordan Ledvina
Jordan Ledvina 2013년 1월 15일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric
Cedric 2013년 1월 15일
편집: Cedric 2013년 1월 15일
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

추가 답변 (1개)

Jordan Ledvina
Jordan Ledvina 2013년 1월 16일
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
Cedric
Cedric 2013년 1월 16일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by