필터 지우기
필터 지우기

Using Struct in Matlab

조회 수: 3 (최근 30일)
Abraham
Abraham 2018년 9월 23일
댓글: Stephen23 2018년 9월 25일
I need help on how to use a struct in Matlab. I need to create a function get_mas, the intent of this function is to sum the mass of all components of a rover and return the result. This function computes rover mass in kilograms. It accounts for the following chassis, power subsystem, science payload, and six wheel assemblies, which itself is comprised of a motor, speed reducer, and the wheel itself. I have attached the table of what I need to do. All i need is walk through on how to get this done. I have never used the struct feature before. Thanks a lot
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 23일
@Abraham: what is your question for us?
Start by reading the documentation, and experimenting with some small examples.

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2018년 9월 24일
편집: James Tursa 2018년 9월 24일
Hints: Suppose myrover is the variable name of your rover. Then
myrover.wheel_assembly(1).wheel.mass <-- Mass of wheel #1
myrover.wheel_assembly(2).wheel.mass <-- Mass of wheel #2
myrover.wheel_assembly(1).speed_reducer.mass <-- Mass of speed reducer #1
myrover.wheel_assembly(2).speed_reducer.mass <-- Mass of speed reducer #2
myrover.wheel_assembly(1).motor.mass <-- Mass of motor #1
myrover.wheel_assembly(2).motor.mass <-- Mass of motor #2
myrover.chassis.mass <-- Mass of chassis
:
etc
So, just type all of the masses out and add them up.
Also, consider this example which might help you with the wheel assembly masses:
>> s(1).mass = 4
s =
1x2 struct array with fields:
mass
>> s(2).mass = 5
s =
1x2 struct array with fields:
mass
>> s(3).mass = 6
s =
1x3 struct array with fields:
mass
>> s.mass % <-- This syntax returns all of the masses individually
ans =
4
ans =
5
ans =
6
>> [s.mass] % <-- This syntax concatenates all of the masses into one vector
ans =
4 5 6
Maybe you can use this syntax to make working with the multiple wheel assembly masses easier.
Your function could look like this:
% get_mas calculates the total mass of a rover vehicle
function mass = get_mas(myrover)
% put your code for calculating the total mass here
end
  댓글 수: 2
Abraham
Abraham 2018년 9월 24일
Thank you. I was doing something like this when I couldn't find any help but now, I can use your example to modify my codes. Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Engines & Motors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by