How to pass arrays to class properly?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
NoYeah
am 24 Apr. 2020
Bearbeitet: NoYeah
am 24 Apr. 2020
I have 3 arrays
a=[[1;2;3],[4;5;6],[7;8;9]]
b=[[3,2,1],[6,5,4],[9,8,7]]
c=[3 6 9]
inst=myclass;
inst.myfunc(arr1, arr2, arr3);
and I delivered it to my class
classdef myclass
properties
a;
b;
c;
end
methods
function obj=myfunc(obj, arr1, arr2, arr3)
obj.a=arr1;
obj.b=arr2;
obj.c=arr3;
end
end
end
the result says
myclass.a=[]
same for the other properties
why this happens? and how to fix it?
I had thought that this is something to do with class instructor
so I made empty class constructer and added it to methods
function obj=myclass(obj)
end
and fixed 'inst' too
inst=myclass();
same error had occured.
How to pass arrays to function properly?
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 24 Apr. 2020
Bearbeitet: Ameer Hamza
am 24 Apr. 2020
The class you defined is called value class in MATLAB. The didn't directly modify the instance of the class. You need to assign it yourself
inst = inst.myfunc(arr1, arr2, arr3);
Your current code will also work, if you define your class as handle class, in that case
classdef myclass < handle
properties
a;
b;
c;
end
methods
function obj=myfunc(obj, arr1, arr2, arr3)
obj.a=arr1;
obj.b=arr2;
obj.c=arr3;
end
end
end
Read more about handle class here: https://www.mathworks.com/help/matlab/handle-classes.html
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays 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!