How to make a conditional statement for blood types?

6 views (last 30 days)
I tried to make a conditional statement so that when someone enters their blood type, they would get the results from which blood type they can receive and which they can donate to, I haven't finished it but when I type "A+" to answer the question it gives me the error message below. Can someone explain how to fix this for me.
clc,clear
bloodtype = ["A+", "A-", "AB+", "AB-", "B+", "B-", "O+", "O-", "everyone"];
bloodtype = input('what is your blood type?: ');
if bloodtype == "A"
disp('You can give to A+, and AB+');
disp('You receive from A+, A-, O+, and O-')
end
what is your blood type?: A+
A+
Error: Invalid expression. Check for missing or extra characters.

Answers (1)

Cameron
Cameron on 2 Mar 2023
Edited: Cameron on 2 Mar 2023
When you put a "+" MATLAB is expecting you to perform a mathematical operation. To get around this, you can input put quotes around your input to indicate it is a string such as "A+" or 'A+'. The difference between these is a string vs character array.
This is probably the way I would do it.
Input = ["A+","A-","B+","B-","O+","O-","AB+","AB-"];
Donor = {{"A+","AB+"},{"A-","A+","AB-","AB+"},{"B+","AB+"},{"B-","B+","AB-","AB+"},...
{"O+","A+","B+","AB+"},...
{"All Blood Types"},{"AB+"},{"AB-","AB+"}};
Receive = {{"A+","A-","O+","O-"},{"A-","O-"},{"B+","B-","O+","O-"},...
{"B-","O-"},{"O+","O-"},{"O-"},{"All Blood Types"},{"AB-","A-","B-","O-"}};
[indx,tf] = listdlg('PromptString','Select a blood type',...
'SelectionMode','single','ListString',Input);
if tf == 0; return; end
disp('Your blood type: ' + strjoin(string(Input{indx}),','))
disp('You can give to: ' + strjoin(string(Donor{indx}),','))
disp('You can receive from: ' + strjoin(string(Receive{indx}),','))

Categories

Find more on Biological and Health Sciences 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!