Increment a class property everytime calling a class method
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Tony Rochelle
il 20 Giu 2020
Modificato: per isakson
il 21 Giu 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 Commenti
Risposta accettata
J. Alex Lee
il 20 Giu 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!