How to use Cell Structures and fields
Show older comments
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 Comment
Durga Yenikepalli
on 18 Dec 2020
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:

Answers (0)
Categories
Find more on Other Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!