Increment a class property everytime calling a class method

15 views (last 30 days)
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

Accepted Answer

J. Alex Lee
J. Alex Lee on 20 Jun 2020
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

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!