Anyway to make this more concise?
Show older comments
Here is my code:
data = readtable('alltimeteams.xlsx');
choice = listdlg('SelectionMode','Single', ...
'ListString', {'Atlanta Hawks', 'Boston Celtics'});
switch choice
case 1 % each statistic for the Hawks and their output
chosenstat = input("What statistic would you like to view? Please choose from Founding year, Age, Wins, Losses, Win-loss ratio, Championships, Abbreviation, Previous names. (CASE SENSITIVE)", 's');
if chosenstat == "Founding year"
fprintf("The founding year of the Atlanta Hawks is %s\n", data.From{1})
elseif chosenstat == "Age"
fprintf("The number of years the Atlanta Hawks have been established is %d\n", data.Yrs(1,1))
elseif chosenstat == "Wins"
fprintf("The number of wins for the Atlanta Hawks is %d\n", data.W(1,1));
elseif chosenstat == "Losses"
fprintf("The number of losses for the Atlanta Hawks is %d\n", data.L(1,1));
elseif chosenstat == "Win-loss ratio"
fprintf("The win-loss ratio for the Atlanta Hawks is %.3f\n", data.W_L_(1,1));
elseif chosenstat == "Championships"
fprintf("The number of championships won by the Atlanta Hawks is %d\n", data.Champ(1,1));
elseif chosenstat == "Abbreviation"
fprintf("The abbreviation for the Atlanta Hawks is %s\n", data.TeamAbbr{1});
elseif chosenstat == "Previous names"
fprintf("The number of previous names for the Atlanta Hawks is %d\n", data.Prev_Names_excl_Current_(1,1));
else
fprintf("That is not a valid statistic (it is case sensitive)")
end
end
Here is the data:

This code works, however im going to have 30 different cases, and this is a very "clunky" way to do this.
I was thinking to make a function for this, but how would I extract team specific data from the table in the function?
Please let me know of any ideas.
P.S The code for the Celtics is the same but obviously the statistics and outputs are specific to them
Accepted Answer
More Answers (1)
Rather than depending on users typing in the strings exactly as you're looking for them, if you know what variables will be in your table and know how you want to display the results, I would set the table's VariableNames property appropriately, use them in the display, and then use those variable names to index into the table. Let's take a sample table.
load patients
bloodPressure = table(LastName, Systolic, Diastolic);
head(bloodPressure)
Let's limit it to the first eight names/values, to avoid the double arrays I'll create later being too long.
bloodPressure = bloodPressure(1:8, :);
We can get the variable names.
varnames = bloodPressure.Properties.VariableNames
Now if I wanted to get all the Systolic values:
variableToRetrieve = varnames{2}
allSystolicValues1 = bloodPressure.(variableToRetrieve) % double vector, or
allSystolicValues2 = bloodPressure(:, variableToRetrieve) % 1 variable table, or
allSystolicValues3 = bloodPressure{:, variableToRetrieve} % double vector
If I wanted to have custom messages, I could put them in something like a dictionary or a struct array.
messages = struct('LastName', 'Patient''s last name', ...
'Systolic', 'Systolic blood pressure', ...
'Diastolic', 'Diastolic blood pressure');
firstPatient = bloodPressure(1, :);
for k = 1:length(varnames)
name = varnames{k};
fprintf("The variable %s of firstPatient is " + firstPatient{1, name} + ...
" and it represents %s.\n", name, messages.(name))
end
Note that the only places I hard-coded the names of the variables in the table were when I actually created it and when I created the struct array containing the messages. But you're already doing that in your if / elseif / else / end "switchyard".
1 Comment
dpb
on 18 Nov 2025
"Rather than depending on users typing in the strings ..."
We fixed that some several questions ago, Steven; @Ayaan switched his(?) code from using input that did require typing in the strings to using a listdlg dropdown list from which to select the desired inputs. The code presented still has remnants of the original string matching in it including the admonition about case-sensitive and possibly unknown matches that are avoided this way.
It's probable there's still better choices, partciularly in that at present the user can get one and only one piece of data each time the code is run, but at least it does remove the direct typing.
I don't disagree that the table variable names could be renamed to match although with the list dialog return, one can simply use the list number instead so it doesn't matter too much although one could then also generalize the statistic in the display by using the VariableNames property, agreed.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!