Anyway to make this more concise?
70 ビュー (過去 30 日間)
古いコメントを表示
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
2 件のコメント
Stephen23
2025 年 11 月 18 日 15:41
"but how would I extract team specific data from the table in the function?"
採用された回答
dpb
2025 年 11 月 18 日 15:58
編集済み: dpb
2025 年 11 月 18 日 16:49
You forgot to attach the input file, there's nothing to be done with just an image.
The way to make it more concise is to have the user specify the franchise and statistic(s) desired and then return those by matching the franchise and desired columns and return the data for those.
% caution air code...
data = readtable('alltimeteams.xlsx');
franchise=listdlg('SelectionMode','Single', 'ListString', data.Franchise); % all possible franchises
STATS=["Founding year","Age","Wins","Losses","Win-loss ratio","Championships","Abbreviation","Previous names"];
statistic=listdlg('SelectionMode','Single', 'ListString', STATS); % ditto statistics
teamData=data(franchise,:); % all franchise data
switch statistic
case 1
fprintf("The founding year of the %s is %d\n", teamData.Franchise,teamData.(statistic))
case 2
fprintf("The number of years the %s have been established is %d\n",teamData.Franchise,teamData.(statistic))
case 3
fprintf("The number of wins for the %s is %d\n",teamData.Franchise,teamData.(statistic))
etc, etc., ...
end
Generalize the statements to use the franchise name as data instead of each alone.
You could also make it somewhat more general by saving the format statements as an array of strings and then using a lookup indexs for the specific one by the index into the choice of statistic.
The general principle is to remove data from code entirely.
8 件のコメント
dpb
2025 年 11 月 18 日 23:06
teamname = teamData(:,1); % extracts the name of the franchise
teamnamearray = table2array(teamname); % converts teamname into an array
name = string(teamnamearray); % converts teamnamearray into a string
The above would instead be
teamname=teamData.Franchise;
but instead of making local copies of the same data, just use the reference directly wherever you need it.
If you want a string, then make it one...
data=convertvars(data,'Franchise','string');
or, better yet, declare the desired string type when reading the input table to begin with.
その他の回答 (1 件)
Steven Lord
2025 年 11 月 18 日 16:05
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 件のコメント
dpb
2025 年 11 月 18 日 16:14
"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.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!