Increment a class property everytime calling a class method

조회 수: 1 (최근 30일)
Tony Rochelle
Tony Rochelle 2020년 6월 20일
편집: per isakson 2020년 6월 21일
Hey Guys,
I am trying to write a program which read different txt-files, whenever I call a class method.
So for example
%%%%%%%%%%%%
classdef Foo
properties
counter=0;
end
methods
% Standard constructor
function1
%function 2 (should read the text file and increment the counter property)
function 2
counter=counter+1;
for i=counter to counter+N..
readfile
end
counter = i;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So basically when I'm calling function 2, it reads the file until a number N. After it read it, it sets the property counter to the last index. And whenever I call the function 2 again, it should read the files starting from counter+1. Is there a way to work with set/get. I don't understand the documentation tbh
Thank you guys

채택된 답변

J. Alex Lee
J. Alex Lee 2020년 6월 20일
subclass from the handle class (default is value class), and you can update properties of the instance without explicitly overwriting the instance. It's not clear what your loop is doing (N is not defined), i dont think you need it...or it might make more sense to loop from outside the method.
classdef Foo < handle
properties
counter = 0
end
methods
function this = Foo(varargin)
% constructor
end
function readFile(this,file)
% do whatever you need to do
this.counter = this.counter + 1;
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by