How to find cell match in matlab table and exctract data from table

57 views (last 30 days)
Let's say I have a table. And a cell array, with
names = {'Smith, 'Williams}
Gender Age Smoker BloodPressure
______ ___ ______ _______________
Smith Male 38 true 124 93
Johnson Male 43 false 109 77
Williams Female 38 false 125 83
Jones Female 40 false 117 75
Brown Female 49 false 122 80
Now I would like to find the names match from the table and extract the values of other variables Gender, Age, Smoker, BloodPressure. I tried to use isfield, but this does not work and I am running out of ideas. I was plaaning to pull the row number and then all the column according to the row.
if isfield(Table,Names);
end
I tried finding

Accepted Answer

Star Strider
Star Strider on 3 Feb 2016
No explicit comparisons are necessary. It’s actually all in the documentation:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...
'RowNames',LastName);
names = {'Smith', 'Williams'};
T_result = T(names,:) % Get Data For Specific Patients
T_result =
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Williams 38 64 131 125 83
  4 Comments

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 10 Feb 2016
Gert, no offense intended, but it's your data format that I would describe as "awful". Using StarStrider's code, Here's the first few rows of each of those tables:
>> ShipData(1:6,:)
ans =
RegisterNumber IMONumber ShipName CallSign Typeservice Owner ConnectingDistrict Flag PortofRegistry MainClassSymbols ServiceNotations NavigationNotations AdditionalClassNotations Machinery GrossTonnage69 NetTonnage69 Deadweight OverallLength LPP Breadth Depth Draught Freeboard Builder PlaceofbuildCountry DateofBuild YardN HullMaterial NbofWatertightComp NumberofContDecks NumberofTanks MainProducts CargoHandling LBC PropellingType Licence DateofBuild1 Builder1 PlaceofBuildcountry TotalPowerkW TotalPowerHP InternalCombustionEngine Boiler PowerVoltage LightingVoltage Frequency MainEngineDrivenGenerators DieselGenerators EmergencyGenerators Propellingsystem Speed FuelCapacity
__________________ _____________ _______________ __________________________________________________________________________________ ____________________ ___________________________________________________ ________________________________________ _______________________ ______________________ _____________________ ____________________________________ _________________________ __________________________________________________ __________________________________________________________ ___________________________________________________ ___________________ ___________________ _____________ _________________ _________________ __________ ____________ ____________________________________ ________________ ___________________ __________________________________ __________________ __________________________ _________________________ _________________________ ________________________ __________________________ __________________________________________ ________________________________ __________________ _____________ ____________________________________________________ ___________________________ ___________________ _________________________________________________ _____________________________________________________________ _______________________________ ________________________________ __________________________________________________________________ ________________________________ ________________________________________________________ __________________________ ________________ ___________________ ________________ _____ ____________
'Register Number:' 'IMO Number:' 'Ship Name:' 'Call Sign:' 'Type & service:' 'Owner:' 'Connecting District:' 'Flag:' 'Port of Registry:' 'Main Class Symbols:' 'Service Notations:' 'Navigation Notations:' 'Additional Class Notation(s):' 'Machinery:' 'Gross Tonnage 69:' 'Deadweight:' 'Overall Length:' NaN 'Breadth:' 'Depth:' 'Draught:' 'Freeboard:' 'Builder:' 'Date of Build:' 'Yard NĀ°:' 'Hull Material:' 'Number of Holds:' 'Total Capacity of Holds:' 'LBC:' 'Propelling Type:' 'Total Power (kW):' 'Total Power (HP):' 'Internal Combustion Engine:' 'Propelling system:' 'Speed:' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''
'12536D' '9180401' 'CEMSOL' 'C4WH2' 'General cargo ship' 'MS 'CEMSOL' Schifffahrtsgesellschaft mbH & Co. KG' 'HAMBURG (HBR)' 'CYPRUS' 'LIMASSOL' 'I HullĀ Ā Ā Mach' 'General cargo ship /cement carrier' 'Unrestricted navigation' ' AUT-UMS , ICE CLASS IA' 'MACH' '3239' '4529 ton' '96 m' 92.4 '13.6 m' '7.9 m' '6.13 m' '1770 mm' 'PETERSWERFT Wewelsfleth GmbH & Co.' '01 May 1998' '655' 'Steel' '4' '0.00' '9927' 'Diesel' '2640 kW' '3587 HP' '(1) T - cyl - 0.00 cm x 0.00 cm at rpm' '1 Screw Propeller Solid 10.00' '12.5 kn' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''
'Register Number:' 'IMO Number:' 'Ship Name:' 'Call Sign:' 'Type & service:' 'System Ship Type:' 'Owner:' 'Connecting District:' 'Flag:' 'Port of Registry:' 'Main Class Symbols:' 'Service Notations:' 'Navigation Notations:' 'Additional Class Notation(s):' 'Machinery:' 'Gross Tonnage 69:' 'Net Tonnage 69:' NaN 'Overall Length:' 'LPP:' 'Breadth:' 'Depth:' 'Draught:' 'Freeboard:' 'Builder:' 'Date of Build:' 'Yard NĀ°:' 'Hull Material:' 'Nb of Watertight Comp.:' 'Number of Cont. Decks:' 'Number of Holds:' 'Total Capacity of Holds:' 'LBC:' 'Propelling Type:' 'Licence:' 'Builder:' 'Place of Build (country):' 'Total Power (kW):' 'Total Power (HP):' 'Internal Combustion Engine:' 'Boiler:' 'Frequency:' 'Main Engine Driven Generators:' 'Diesel Generators:' 'Emergency Generators:' 'Propelling system:' 'Speed:' 'Fuel Capacity:' '' '' '' ''
'14968W' '9376036' 'RUTH' '5BEG2' 'Container ship' 'Other cargo ship' 'ms 'Ruth' Jens und Waller GmbH & Co KG' 'HAMBURG (HBR)' 'CYPRUS' 'LIMASSOL' 'I HullĀ Ā Ā Mach' 'Container ship' 'Unrestricted navigation' ' AUT-UMS (SS) , MON-SHAFT , ICE CLASS IA , INWATERSURVEY' 'MACH' '9981' '6006' 11253 '134.4 m' '125.8 m' '22.5 m' '11.3 m' '8.71 m' '2609 mm' 'Pella Sietas GmbH' '08 May 2008' '1284' 'Steel' '4' '1' '4' '0.00' '31985' 'Diesel' 'Caterpillar' 'Caterpillar' 'Rostock' '8400 kW' '11413 HP' '(1) 4T - 9 cyl - 43.00 cm x 61.00 cm at 127 rpm' '1 CHR 221.50 mĀ² / 10.00 bar1 CHO 51.70 mĀ² / 10.00 bar' '50 Hz' '1 - 1628 kVA - 1302 kW' '1 - 1390 kVA - 1112 kW - 1163 HP1 - 1810 kVA - 1448 kW - 1386 HP' '1 - 1155 kVA - 924 kW - 462 HP' '1 Screw Propeller Controllable pitch 10.00 at 127 rpm' '18.5 kn' '977 mĀ³' '' '' '' ''
'Register Number:' 'IMO Number:' 'Ship Name:' 'Ex Names:' 'Call Sign:' 'Type & service:' 'System Ship Type:' 'Owner:' 'Connecting District:' 'Flag:' 'Port of Registry:' 'Main Class Symbols:' 'Service Notations:' 'Navigation Notations:' 'Additional Class Notation(s):' 'Machinery:' 'Gross Tonnage 69:' 69 'Deadweight:' 'Overall Length:' 'LPP:' 'Breadth:' 'Depth:' 'Draught:' 'Freeboard:' 'Builder:' 'Date of Build:' 'Yard NĀ°:' 'Hull Material:' 'Nb of Watertight Comp.:' 'Number of Cont. Decks:' 'Number of Holds:' 'Total Capacity of Holds:' 'LBC:' 'Propelling Type:' 'Licence:' 'Builder:' 'Place of Build (country):' 'Total Power (kW):' 'Total Power (HP):' 'Internal Combustion Engine:' 'Boiler:' 'Frequency:' 'Diesel Generators:' 'Emergency Generators:' 'Propelling system:' 'Speed:' 'Fuel Capacity:' '' '' '' ''
'16283B' '9354868' 'STAR MARIELLA' 'STAR MOIRA (2015), STAR MARIELLA (2014), SANTA BARBARA (2006), BULK EIGHT (2006)' 'V7KK3' 'Bulk carrier' 'Bulk carrier' 'STAR TRIDENT XVI LLC.' 'LIMASSOL (LML)' 'MARSHALL ISLANDS' 'MAJURO' 'I HullĀ Ā Ā Mach' 'Bulk carrier BC-A (holds 2,4,6 may be empty) ESP' 'Unrestricted navigation' ' AUT-UMS (SS) , MON-SHAFT , ERS-S , INWATERSURVEY' 'MACH' '43189' 27291 '82226 ton' '228.99 m' '222 m' '32.26 m' '20.03 m' '14.43 m' '5657 mm' 'TSUNEISHI SHIPBUILDING CO., LTD.' '30 Mar 2006' 'S1382' 'Steel' '7' '1Machinery Aft' '7' '97186.10' '143449' 'Diesel' 'MAN B&W;' 'MITSUI ENGINEERING & SHIPBUILDING CO., LTD. Tamano' 'Tamano (JPN)' '9800 kW' '13315 HP' '(1) 2T - 7 cyl - 50.00 cm x 200.00 cm at 113 rpm' '1 CHM 212.00 mĀ² / 7.00 bar' '60 Hz' '3 - 550 kVA - 440 kW - 652 HP' '1 - 100 kVA - 80 kW - 135 HP' '1 Screw Propeller Solid LB 10.00 at 113 rpm' '14.5 kn' '2920.5 mĀ³' '' '' '' ''
>> ship_evs_tab(1:6,:)
ans =
Shipname MMSI Nr_of_events
_____________ ___________ ____________
'VIKING XPRS' '276813000' 801
'STAR' '276672000' 719
'FINLANDIA' '230628000' 512
'SUPERSTAR' '276747000' 434
'AMBAL' '273355410' 68
'MERILIN' '276696000' 45
It looks like ShipData has what ought to be column headers in every other row, and some rows seem to be out of sync, containing "kinds" of values that other rows do not. I can't say how to fix this, because I don't know where the data came from, but I think that's the source of your frustration.
If you had one row in ShipData for each ship, then appending data to that from ship_evs_tab would be a simple join operation, one line of code. And if you had the ship names as the row names of each table, and there's a 1-to-1 match on the ship names, it's just a horizontal concatenation.
One small suggestion about StarStrider's code:
ShipEvsNameCell = table2cell(ship_evs_tab(:,1));
ShipDataNameCell = table2cell(ShipData(:,3));
is probably more clearly written as
ShipEvsNameCell = ship_evs_tab.Shipname;
ShipDataNameCell = ShipData.ShipName;
Hope this helps.
  1 Comment
Peter Perkins
Peter Perkins on 10 Feb 2016
Gert, it would be interesting to hear where these data are coming from, and how they got into MATLAB in that format, if you are able to say.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!