How to define objects from a cell-array table in Matlab

1 view (last 30 days)
Hi Matlab Master,
I have a table of objects, which belong to different typs. I also have a table of parts, which build up the objects.
typ : motor, car, truck, boat
Objects= {'Name ' 'weight' 'height' 'lengthth' 'depth' 'Parts'
'motor Motor1' 100 120 180 40 'm1,m2'
'motor Motor2' 110 110 190 50 'm1,m2'
'car Car1' 600 140 330 210 'c1,c2,c3'
'truck Truck1' 3000 250 500 230 't1,t2,t3'
'boat Boat1' 5000 500 1000 300 'b1,b2'}
Parts = {'Name ' 'weight' 'height' 'lengthth' ' depth'
'Motor1-m1' 30 23 46 53
'Motor1-m2' 70 56 24 65
'Motor2-m1' 50 76 86 50
'Motor2-m2' 60 35 76 23
'Car1-c1' 100 140 330 210
'Car1-c2' 200 31 312 23
'Car1-c3' 300 78 123 12
'Truck1-t1' 234 234 412 143
'Truck1-t2' 154 123 423 230
'Truck1-t3' 4533 342 234 154
'Boat1-b1' 4545 234 452 265
'Boat1-b2' 3456 342 645 211}
  • What I need to do is a program, that helps me collect data from the tables above, so that:
If I call "typ"
Program returns:
typ ={'motor'
'car'
'truck'
'boat'}
If I call "motor"
Program returns :
motor = {'Motor1'
'Motor2'}
If I call "Motor1"
Program returns :
Motor1 = {100 120 180 140 'm1,m2'}
If I call Motor1-m1
Program returns:
Motor1-m1={30 23 46 53}
And so on for every typ, every object, and every part. Thanks in advance for your advices!
  1 Comment
Sean de Wolski
Sean de Wolski on 2 Aug 2013
If your SMS subscription is current, you can download the R2013b Prerelease which has some things you might find useful for tables.

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 2 Aug 2013
clear
Objects= {'Name ' 'weight' 'height' 'lengthth' 'depth' 'Parts'
'motor Motor1' 100 120 180 40 'm1,m2'
'motor Motor2' 110 110 190 50 'm1,m2'
'car Car1' 600 140 330 210 'c1,c2,c3'
'truck Truck1' 3000 250 500 230 't1,t2,t3'
'boat Boat1' 5000 500 1000 300 'b1,b2'}
Parts = {'Name ' 'weight' 'height' 'lengthth' ' depth'
'Motor1-m1' 30 23 46 53
'Motor1-m2' 70 56 24 65
'Motor2-m1' 50 76 86 50
'Motor2-m2' 60 35 76 23
'Car1-c1' 100 140 330 210
'Car1-c2' 200 31 312 23
'Car1-c3' 300 78 123 12
'Truck1-t1' 234 234 412 143
'Truck1-t2' 154 123 423 230
'Truck1-t3' 4533 342 234 154
'Boat1-b1' 4545 234 452 265
'Boat1-b2' 3456 342 645 211}
Parts(:,1)=regexprep(Parts(:,1),'-','_')
a=cellfun(@(x) regexp(x,'\s','split'),Objects(2:end,1),'un',0);
b=cellfun(@(x) char(x(1)),a,'un',0);
c=cellfun(@(x) char(x(2)),a,'un',0);
[type,ii,jj]=unique(b,'stable');
w=1;
for k=1:numel(type)
type1.(type{k})=c(jj==k);
for l=1:numel(type1.(type{k}))
x=type1.(type{k})(l);
w=w+1;
type2.(x{1})=Objects(w,2:end);
end
end
for k=2:size(Parts,1)
type3.(Parts{k,1})=Parts(k,2:end);
end
%-----------------Test- type1-------------------------------
type1
type1.motor
type1.car
%---------------Test type2---------------------------------
type2
type2.Motor1
type2.Motor2
type2.Car1
%---------------Test type3---------------------------------
type3
type3.Motor1_m1

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!