How to use Cell Structures and fields
이전 댓글 표시
So I'm relatively new with MATLAB and am trying to make a program to choose your car based on mpg, price etc. I have
Car(1).name = 'BMW 3 Series'
Car(1).type = 'Sedan'
Car(1).mpg = 30
Car(1).price = 40,000
Car(1).seats= 5
for example and other vehicles and want to narrow it down. How would I say I want only cars with <35 mpg and that cost $40k or under and then a car with say 5 seats and so on until all the questions are answered to reveal a car(s).
댓글 수: 1
Durga Yenikepalli
2020년 12월 18일
Hi Jake,
My understanding is that you are trying to choose car(s) that satisfies specific condition.
One way could be to use Logical indexing with structures. Refer below code.
% lets assume i have 3 cars
Car(1).name = 'BMW 3 Series';
Car(1).type = 'Sedan';
Car(1).mpg = 30;
Car(1).price = 40,000;
Car(1).seats= 5;
Car(2).name = 'XYZ';
Car(2).type = 'mini';
Car(2).mpg = 25;
Car(2).price = 15,000;
Car(2).seats= 3;
Car(3).name = 'ABC';
Car(3).type = 'XUV';
Car(3).mpg = 50;
Car(3).price = 70,000;
Car(3).seats= 7;
% specify your condition
x= [Car.mpg] < 35 & [Car.seats] == 5 & [Car.price] <= 40,000;
carsList = Car(x);
Ans:

답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!