Why is this showing the first fprintf statement?

32 views (last 30 days)
Ayaan
Ayaan on 16 Nov 2025 at 21:47
Edited: dpb on 17 Nov 2025 at 14:10
Code:
chosenstat = input("What statistic would you like to view? ", 's')
Error using input
Support for user input is required, which is not available on this platform.
% ignore the input error, it works on the actual program
if chosenstat == "Founding year"
AHyear = data(1,2); % this is 1968
AHyeararray = table2array(AHyear);
fprintf("The founding year of the Atlanta Hawks is %s\n", AHyeararray{:})
elseif chosenstat == "Number of years established"
AHestablished = data(1,4) % this is 57
AHestablishedarray = table2array(AHestablished)
fprintf("The number of years the Atlanta Hawks have been established is %s\n", AHestablishedarray{:})
end
This is what shows in the command window:
What statistic would you like to view? Number of years established
The founding year of the Atlanta Hawks is 1968
>>
Why is it that the first fprintf statement is showing up but not the second one?

Answers (1)

dpb
dpb on 16 Nov 2025 at 22:19
Edited: dpb on 16 Nov 2025 at 22:31
You would have to rerun the code to enter the other condition to do the else if clause instead of the first. chosenstat will only be one value each time the code is run.
If you want both answers even if only the one was asked for, you'll have to put it into the code to do so...
chosenstat = input("What statistic would you like to view? ")
% ignore the input error, it works on the actual program
if chosenstat == "Founding year"
AHyear = data(1,2); % this is 1968
AHyeararray = table2array(AHyear);
fprintf("The founding year of the Atlanta Hawks is %s\n", AHyeararray{:})
AHestablished = data(1,4) % this is 57
AHestablishedarray = table2array(AHestablished)
fprintf("The number of years the Atlanta Hawks have been established is %s\n", AHestablishedarray{:})
elseif chosenstat == "Number of years established"
% what is required for this case goes here -- and it "knows nothink!" about
% what is in the previous case construct so it all will have to be duplicated
% you need to write a generic routine to call with the given parameter instead
end
You stll need the second 'S' argument here as noted in the previous answer to the same problem and as also discussed there, this is a very painful interface for such input.
Also, if you have a table containing the data there's no reason/need for table2array to access it; use the table name with dot addressing to return the variable; indexing into the array for the row.
Show the structure of your data table and somebody can illustrate how to access it or restructure it to be more accessible directly. Study table and particularly <How to access data in a table>
  6 Comments
Ayaan
Ayaan on 16 Nov 2025 at 23:44
Hang on, I seem to have fixed it, thanks for your help though
dpb
dpb on 16 Nov 2025 at 23:46
Edited: dpb on 17 Nov 2025 at 14:10
Note also that the selection of a single block from the input doesn't need the complexity of the if ... elseif block. Each can simply be a standalone if ... end.block. This also is the kind of logic the switch block was invented for as demonstrated before as well.

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!