필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Ali Mania
Ali Mania 2013년 8월 2일
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
Sean de Wolski
Sean de Wolski 2013년 8월 2일
If your SMS subscription is current, you can download the R2013b Prerelease which has some things you might find useful for tables.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 2일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by