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

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

 채택된 답변

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

My problem exists actually on a bigger scale. You are right of course, this is the way to access data in tables. I was trying to ask for a hint so I could solve the problem on my own, but it over my head. I have 2 .m files, one contains instances and the other data. Now i need to take the names from the Instances.m table and search for the in the ShipData.m. If there is a match extract the row values and also row-1 which shows the variable names. The table format is awful, but at the moment only option. My approach so far, create an array of instances and then search. But in every way I try, I can't get results. If you could provide me with a tip, that would be helpful.
The tables aren’t that difficult to work with, and in this instance, may be the best option.
This gets the information you want (as I understand). The ‘Look At ...’ lines let you see what the code recovers, since it otherwise would be difficult to read. They are not an actual part of the code, and can be deleted when you don’t need them.
It would be easy to interlace ‘Results_Row_Titles’ and ‘Results_Ship_Data’, but I kept them separate here, since that may actually be easier to work with this way. The same row numbers match to both arrays. I kept them in the original order, using the 'stable' flag. If you want them alphabetised by ship name, remove that flag.
Now, my code:
D1 = load('Gert Lõhmus ShipData.mat');
ShipData = D1.ShipData;
D2 = load('Gert Lõhmus Instances.mat');
ship_evs_tab = D2.ship_evs_tab;
ShipEvsNameCell = table2cell(ship_evs_tab(:,1));
ShipDataNameCell = table2cell(ShipData(:,3));
[ShipNames, EvsIdx, DataIdx] = intersect(ShipEvsNameCell, ShipDataNameCell, 'stable');
Results_Ship_Data = ShipData(DataIdx,:);
Results_Row_Titles = ShipData(DataIdx-1,:);
Look_At_Data = Results_Ship_Data(1:5,:); % See Matching ‘ShipData’ Data (Delete)
Look_At_Titles = Results_Row_Titles(1:5,:); % See Matching ‘ShipData’ Row Titles (Delete)
Thank you!

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

추가 답변 (1개)

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

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.

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

카테고리

도움말 센터File Exchange에서 Handle Classes에 대해 자세히 알아보기

질문:

N/A
2016년 2월 3일

댓글:

2016년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by