Can I use a variable name to make logical comparison?
Show older comments
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
4 Comments
"I have created the above array, months, and assigned each month to the number of days in it."
Did you define twelve separate variables named "January", "February", etc. ?
If, so, do NOT do that. Using meta-data (e.g. month names) in variable names makes acessing data slow, complex, inefficient, buggy, and difficult to debug:
Much better data design would store the meta-data in one variable, e.g. as text in a cell/string array, in a column of a table, or as fieldnames of a structure.
Snr Jhay
on 15 Nov 2021
Image Analyst
on 15 Nov 2021
Variable names can contain letters and numbers. Use whatever letters and numbers make for a descriptive variable name. Single letters are usually not descriptive except for really simple cases like x and y and k for a loop iterator.
You never defined January in your code so it doesn't know what that is. You need to assign it to a string or number
January = 'January'; % Character array.
January = "January"; % String Variable.
January = 1; % Double/number.
You can use numbers instead of strings if you want -- your choice.
Snr Jhay
on 16 Nov 2021
Answers (3)
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
%month = input('Enter the month: ');
month = "May"
k = find(months==month)
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))
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)
7 Comments
And completing this very simple task:
y = 2021;
m ='May';
mnm = datetime(y,1:12,1,'Format','MMMM') % better way to do this?
[~,idx] = ismember(m, mnm)
eom = eomday(y, 1:12) % nice and easy!
eom(idx)
Snr Jhay
on 16 Nov 2021
Image Analyst
on 16 Nov 2021
@Snr Jhay not sure what level you're at in your knowledge of MATLAB but you can try this:
or see this link for more options:
Snr Jhay
on 16 Nov 2021
Snr Jhay
on 19 Nov 2021
Stephen23
on 19 Nov 2021
"so the truth is I'm learning Matlab using Matlab For Engineers by Chapman"
Over the years I have read several books about "learning MATLAB" and without exception they contained outdated, incorrect or misleading advice, and used terminology inconsistent with the MATLAB documentation.
A much more reliable source is: https://www.mathworks.com/help/matlab/getting-started-with-matlab.html
Snr Jhay
on 19 Nov 2021
Categories
Find more on Logical 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!