Increment a class property everytime calling a class method
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!