WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Automatic update of properties

 

Tbim
Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 13.03.19
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 13.03.2019, 17:40     Titel: Automatic update of properties
  Antworten mit Zitat      
Hi everybody,

i have a question regarding dependent properties in MATLAB.
On the MATLAB Website i found the following code:

Code:
classdef SquareArea
   properties
      Width
      Height
   end
   properties (Dependent)
      Area
   end
   methods
      function a = get.Area(obj)
         a = obj.Width * obj.Height;
      end
   end
end


Question 1: Why do i need to add "(Dependent)" behind "properties"? I commented out the "(Dependend)" and tested the automatic updating of "Area" --> It also worked. So why do i need to add "Dependend"?

Question 2: How does this automatic updating work at all without calling the get method explicitly by myself? I only Change width and height and the area is updated automatically, even when commenting out the "Dependent". I never call the function get.Area by myself. So how does MATLAB "know" that it has to run the get.Area method after i changed width or height?
Furthermore, the output of the get method is "a" and not "Area". Why is "Area" updated then?

I am a bit lost here and i need your help as i am quite new to object oriented programming in Matlab.
Private Nachricht senden Benutzer-Profile anzeigen


Harald
Forum-Meister

Forum-Meister


Beiträge: 24.432
Anmeldedatum: 26.03.09
Wohnort: Nähe München
Version: ab 2017b
     Beitrag Verfasst am: 13.03.2019, 23:18     Titel:
  Antworten mit Zitat      
Hi,

1. dependent means that a property is not stored explicitly but rather calculated on demand. This requires a get method. So, you could say:
Code:
s = SquareArea;
s.Width = 2;
s.Height = 3;
s.Area % works even though Area has not been set.


2. A get method is called whenever the property is queried. If the only action needed is to return the value of a "regular" property, no get method is needed since this is the default anyway. One exception is a dependent property, since its value is not stored.

Note that this is a German language forum. Thus if you speak German, please ask your questions in German.

Best,
Harald
_________________

1.) Ask MATLAB Documentation
2.) Search gomatlab.de, google.de or MATLAB Answers
3.) Ask Technical Support of MathWorks
4.) Go mad, your problem is unsolvable ;)
Private Nachricht senden Benutzer-Profile anzeigen
 
Tbim
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 13.03.19
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 14.03.2019, 10:53     Titel:
  Antworten mit Zitat      
Danke für die schnelle Antwort!
Ich verstehe aber noch nicht, wieso das ganze auch funktioniert, wenn ich das "(Dependent)" weglasse. Wozu brauche ich es dann überhaupt?

Und zu 2.: Das heißt, es reicht also schon, sich das Objekt "s" anzeigen zu lassen, um die get Methode zu evozieren?
Private Nachricht senden Benutzer-Profile anzeigen
 
Harald
Forum-Meister

Forum-Meister


Beiträge: 24.432
Anmeldedatum: 26.03.09
Wohnort: Nähe München
Version: ab 2017b
     Beitrag Verfasst am: 14.03.2019, 11:20     Titel:
  Antworten mit Zitat      
Hallo,

1. ich denke, vor allem der Übersichtlichkeit halber.

2. wenn disp nicht überladen ist oder im eigenen disp auch die Eigenschaft abgefragt wird: ja.
Du kannst das testen, indem du einen Haltepunkt in die get-Methode setzt oder ein Statement wie folgendes einfügst:
Code:
disp('get wurde ausgeführt')


Grüße,
Harald
_________________

1.) Ask MATLAB Documentation
2.) Search gomatlab.de, google.de or MATLAB Answers
3.) Ask Technical Support of MathWorks
4.) Go mad, your problem is unsolvable ;)
Private Nachricht senden Benutzer-Profile anzeigen
 
Tbim
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 13.03.19
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 14.03.2019, 14:25     Titel:
  Antworten mit Zitat      
Okay, vielen Dank für die Hilfe!
Viele Grüße,
Karl
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.