# Can I use a variable name to make logical comparison?

2 views (last 30 days)
Snr Jhay on 15 Nov 2021
Commented: Snr Jhay on 19 Nov 2021
month = input('Enter the month: ');
months = [January, February, March, April, May, June, July, August, September, October, November, December];
k = find(months==month)
I have created the above array, months, and assigned each month to the number of days in it. I have asked a user to enter a month(in words). Now i want to compare the month the user entered with any of the months in the months array by using the find function, but it always compares the values withing the various months and not the variable names. Is there a workaround for this?
Thank you
Snr Jhay on 16 Nov 2021
Okay. I'm getting that.
Thanks a lot

Chunru on 15 Nov 2021
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
%month = input('Enter the month: ');
month = "May"
month = "May"
k = find(months==month)
k = 5
Snr Jhay on 16 Nov 2021
Nice advice. I'm gonna read more on that, with the efficiency and data storage. Thanks for the time. Hopefully i dont give up on Matlab

Rik on 15 Nov 2021
You could use the string datatype (as Chunru suggested), or use a cellstr (a cell array of char vectors):
%month = input('Enter the month: ','s');
% ^^^^
% don't forget this
month='May';
months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};
k = find(ismember(months,month))
k = 5
Snr Jhay on 16 Nov 2021
They store the numbers of days in the specified month.

Image Analyst on 15 Nov 2021
You don't need find().
Simply use ismember():
%month = input('Enter the month: ','s');
% ^^^^
% don't forget this
month='May';
months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};
[~, index] = ismember(month, months)
index = 5
Snr Jhay on 19 Nov 2021
Okay. I will use the link you just sent to further enhance my studies.
Thank you very much