Info

This question is closed. Reopen it to edit or answer.

How can i acess to created objects out of the class

1 view (last 30 days)
Fil Dim
Fil Dim on 8 Feb 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello. I have a simple Class:
classdef Packung
properties
preis
menge
l
end
methods
function this = Packung(p,m,l)
this.preis = 0;
this.menge = 0;
this.l=l;
%this = this.setPreisMenge(p,m);
end
function this = setPreisEinkaufswagen(this, p)
xxx = this.preis;
display(xxx);
% Objekt2 = setPreisMenge(Objekt2, 2, p);
end
function [this,objekt] = setPreisMenge(this,objekt, p,m)
this.preis = p;
this.menge = m;
objekt.preis = objekt.preis + 100;
end
end
end
now i have another function, in which i want to make a for loop, going through all the objects created from my Class Packung. How to do this?
I tired following: Function PriceProducts is NOT in the Class. Before i started the Function, i made a object from the class: Milch = Packung (1,2,3)
function PriceProducts()
for i=1:10
Product_buying= Milch;
...
end
Though i get an error, Undefined function or variable 'Milch'.
How to manage this? I'd like to predifine all the Objects that need to be used, and then i want to loop them.

Answers (2)

Erik S.
Erik S. on 8 Feb 2015
Don't you have to pass the "Milch" variable to the function PriceProducts? I don't see how the function gets this value otherwise?

Fil Dim
Fil Dim on 8 Feb 2015
Edited: Fil Dim on 8 Feb 2015
Hi Erik. Thanks for your responce.
I want to make a loop through all the Packung Objects. If i make:
PriceProducts(Milk)
for i=1:10
if i = 1
Product_buying= Milk;
elseif i=2
Product_buying = Banana;
...
end
I still have 200 More Products in there. I mean, i can make my function having 200 variables, but i guess there needs to be a smarter way?

Community Treasure Hunt

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

Start Hunting!