Increment a class property everytime calling a class method
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tony Rochelle
am 20 Jun. 2020
Bearbeitet: per isakson
am 21 Jun. 2020
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
0 Kommentare
Akzeptierte Antwort
J. Alex Lee
am 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
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!