Specify nature/type of property

조회 수: 6 (최근 30일)
elevonm
elevonm 2021년 8월 23일
편집: per isakson 2021년 8월 25일
Hello,
I am writing a class in which I have the property 'food'. The property should be composed of a string specifying the type of food and a double specifying the number of calories. How do I specify this to the property? (As an example, just as I have specified 'string' to 'colour', I want to specify something for 'food')
classdef meals
properties
food
colour string
end
end
Thank you in advance for your help!
  댓글 수: 2
Wan Ji
Wan Ji 2021년 8월 23일
You can define another class called food
classdef food
properties
type string
calories double
end
end
Wan Ji
Wan Ji 2021년 8월 23일
And then
classdef meals
properties
food food
colour string
end
end

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

답변 (2개)

Wan Ji
Wan Ji 2021년 8월 23일
First define the food class
Then define meals class
Now run meals in the command window, we get empty class meals with two field properties: food and colour
After that, look into this empty meals named ans
And see ans.food by double click it, food type and calories are there

per isakson
per isakson 2021년 8월 25일
편집: per isakson 2021년 8월 25일
... or something like:
classdef meals
properties
food (1,1) struct = struct( 'type',{'wheat'}, 'calories',{123} );
colour string
end
end
>> m.food
ans =
struct with fields:
type: 'wheat'
calories: 123

카테고리

Help CenterFile Exchange에서 Food Sciences에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by