struct for multiple shapes with similar inputs

4 vues (au cours des 30 derniers jours)
Samantha May
Samantha May le 7 Mar 2023
I am trying to create a struct that takes inputs for a shape (in this case theyre all rectangles) base, height, xbar and ybar of a shape and uses this information to fill in the rest of the vakues for the shape (Area, Ixx, Iyy, etc).
Id like to apply this struct to mulitple shapes
Then, also sum some of the values across the different shapes and store this in a seperate variable from the struct (Ixx_prime = (shape_1.Ixx + shape_2.Ixx + shape_3.Ixx)
This was my approach
% Set up
shape = struct('base', [], 'height', [], 'Area', [], 'xbar', [], 'ybar', [], ...
'Areax2', [], 'Areay2', [], 'Areaxy', [], 'Ixx', [], 'Iyy', [], 'Ixy', [])
shape = struct with fields:
base: [] height: [] Area: [] xbar: [] ybar: [] Areax2: [] Areay2: [] Areaxy: [] Ixx: [] Iyy: [] Ixy: []
shape.base = 10; %
shape.height = 50; %
shape.Area = shape.base*shape.height;
shape.xbar = -15; %
shape.ybar = 30; %
shape.Areax2 = shape.Area*(shape.xbar^2);
shape.Areay2 = shape.Area*(shape.ybar^2);
shape.Areaxy = shape.Area*shape.xbar*shape.ybar;
shape.Ixx = (shape.base*shape.height^3)/12;
shape.Iyy = (shape.base^3*shape.height)/12;
shape.Ixy = 0
shape = struct with fields:
base: 10 height: 50 Area: 500 xbar: -15 ybar: 30 Areax2: 112500 Areay2: 450000 Areaxy: -225000 Ixx: 1.0417e+05 Iyy: 4.1667e+03 Ixy: 0
shapeA = shape
shapeA = struct with fields:
base: 10 height: 50 Area: 500 xbar: -15 ybar: 30 Areax2: 112500 Areay2: 450000 Areaxy: -225000 Ixx: 1.0417e+05 Iyy: 4.1667e+03 Ixy: 0
shapeA.base = 50; %
shapeA.height = 10; %
shapeA.xbar = 0; %
shapeA.ybar = 0 %
shapeA = struct with fields:
base: 50 height: 10 Area: 500 xbar: 0 ybar: 0 Areax2: 112500 Areay2: 450000 Areaxy: -225000 Ixx: 1.0417e+05 Iyy: 4.1667e+03 Ixy: 0
This does not refill the subsequent values for the new shape.
Any ideas for a better approach? I've been looking into classes https://au.mathworks.com/help/matlab/matlab_oop/example-representing-structured-data.html but am unsure how to approach.
Cheers for any advice!

Réponses (1)

Benjamin Kraus
Benjamin Kraus le 7 Mar 2023
There are options for doing this without using classes, but I think classes is probably what you want to do.
I'm not sure I follow what xbar and ybar mean, but let me give an example of a class with just height, base, area and circumference, and you can extrapolate from there.
classdef Shape
properties
Height
Base
end
properties (Dependent)
% These properties are derived from other properties. By tagging
% them as "Dependent" you indicate that the value of these
% properties depends on the value of other properties.
Area
Circumference
end
methods
function a = get.Area(obj)
% This function will execute whenever you attempt to query the
% value of the "Area" property.
a = obj.Height*obj.Base;
end
function c = get.Circumference(obj)
% This function will execute whenever you attempt to query the
% value of the "Circumference" property.
c = 2*(obj.Height+obj.Base);
end
end
end
Once you've written that object, you can create a Shape object and then set the Height and Base, and MATLAB will calculate the Area and Circumference.
s = Shape;
s.Height = 10;
s.Base = 5;
disp(s)
The output will look like this:
s =
Shape with properties:
Height: 10
Base: 5
Area: 50
Circumference: 30
  3 commentaires
Samantha May
Samantha May le 7 Mar 2023
Modifié(e) : Samantha May le 7 Mar 2023
Classes are definetely the way to go!
As I cannot use classes in Live Editor, I have opted to make a class file and reference it. Works flawlessly. This allows me to Sum values across multiple objects of the class Shape!
Absolutely stoked. Thank you
Benjamin Kraus
Benjamin Kraus le 7 Mar 2023
I'm glad you were able to get things up and running. Welcome to the world of Object Oriented Programming (OOP) in MATLAB.
I wish there was a way to define a MATLAB class in-line (both in the Live Editor and in other places), but as you said, that is not currently possible.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by