struct for multiple shapes with similar inputs
2 views (last 30 days)
Show older comments
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.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
shapeA = shape
shapeA.base = 50; %
shapeA.height = 10; %
shapeA.xbar = 0; %
shapeA.ybar = 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!
0 Comments
Answers (1)
Benjamin Kraus
on 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 Comments
Benjamin Kraus
on 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.
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!