Can I use a variable name to make logical comparison?

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 Commenti

Stephen23
Stephen23 il 15 Nov 2021
Modificato: Stephen23 il 15 Nov 2021
"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.
Oh okay. So i should generally just use just letters to represent my variables more efficiently?
Sorry if my question seems lame. I'm quite new to this.
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.
By the way, see my answer below, and the 2 other answers.
Okay. I'm getting that.
Thanks a lot

Accedi per commentare.

Risposte (3)

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

5 Commenti

Thanks a lot.
In my case, the months are not just strings as you've indicated.
They also contain values and so doing this k = find(months==month) might return an error.
Or am I getting something wrong?
Pardon me if I'm having difficulty understanding as I'm quite new to Matlab
Thank you
day = input('Enter the day: ');
month = input('Enter the month: ');
year = input('Enter the year: ');
January = 31;
if leap_year(year) == 1
February = 29;
else
February = 28;
end
March = 31;
April = 30;
May = 31;
June = 30;
July = 31;
August = 31;
September = 30;
October = 31;
November = 30;
December = 31;
months = [January, February, March, April, May, June, July, August, September, October, November, December];
k = find(months==month)
This is what the first part looks like
Stephen23
Stephen23 il 15 Nov 2021
Modificato: Stephen23 il 15 Nov 2021
@Snr Jhay: using lots of separate variables is entirely the wrong approach if you want to write simple, efficient code.
MATLAB is designed for efficient processing of data in arrays, and that is how you should store your data too.
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

Accedi per commentare.

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

3 Commenti

If I do this'
months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};
would the name of the months also carry the values assigned?
Thank you
They will not, nor should they. You should separate data from metadata. What do these arrays store? If the store average temperature per day, wouldn't MeanTemp make more sense as a variable name than January?
They store the numbers of days in the specified month.

Accedi per commentare.

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

7 Commenti

And completing this very simple task:
y = 2021;
m ='May';
mnm = datetime(y,1:12,1,'Format','MMMM') % better way to do this?
mnm = 1×12 datetime array
January February March April May June July August September October November December
[~,idx] = ismember(m, mnm)
idx = 5
eom = eomday(y, 1:12) % nice and easy!
eom = 1×12
31 28 31 30 31 30 31 31 30 31 30 31
eom(idx)
ans = 31
Okay, so the truth is I'm learning Matlab using Matlab For Engineers by Chapman. There was this example that required me to do all we've been discussing. Most of the solutions you guys are putting forth have not been covered yet that's why it all seems strange to me.
Do you have any recommended book I can use in addition to up my game?
@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:
Thank you very much
Thanks guys for all the answers. I'll be learning more to understand better most of the things you've discussed here.
Thanks a lot for your time and patience.
"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.
Okay. I will use the link you just sent to further enhance my studies.
Thank you very much

Accedi per commentare.

Tag

Richiesto:

il 15 Nov 2021

Commentato:

il 19 Nov 2021

Community Treasure Hunt

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

Start Hunting!

Translated by