How do I determine if a column header exists in a table?

36 visualizzazioni (ultimi 30 giorni)
I am performing a readtable() command. Sometimes the table contains a column called "x"; sometimes it is called "y". How would you determine if one column exists or not. I looked through the is* functions but did not see anything that I could use in an if statement.

Risposte (1)

Cris LaPierre
Cris LaPierre il 28 Apr 2025
I would access the table varaible names and check if 'x' is a member or not.
T = readtable('patients.xls')
T = 100x10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' } {'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 false 117 75 {'Fair' } {'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' } {'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 false 121 70 {'Good' } {'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 true 130 88 {'Good' } {'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 false 115 82 {'Good' } {'Moore' } {'Male' } 28 {'St. Mary's Medical Center'} 68 183 false 115 78 {'Excellent'} {'Taylor' } {'Female'} 31 {'County General Hospital' } 66 132 false 118 86 {'Excellent'} {'Anderson'} {'Female'} 45 {'County General Hospital' } 68 128 false 114 77 {'Excellent'} {'Thomas' } {'Female'} 42 {'St. Mary's Medical Center'} 66 137 false 115 68 {'Poor' } {'Jackson' } {'Male' } 25 {'VA Hospital' } 71 174 false 127 74 {'Poor' } {'White' } {'Male' } 39 {'VA Hospital' } 72 202 true 130 95 {'Excellent'} {'Harris' } {'Female'} 36 {'St. Mary's Medical Center'} 65 129 false 114 79 {'Good' } {'Martin' } {'Male' } 48 {'VA Hospital' } 71 181 true 130 92 {'Good' }
varNm = T.Properties.VariableNames
varNm = 1x10 cell array
{'LastName'} {'Gender'} {'Age'} {'Location'} {'Height'} {'Weight'} {'Smoker'} {'Systolic'} {'Diastolic'} {'SelfAssessedHe...'}
any(ismember('X',varNm))
ans = logical
0

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by