Effacer les filtres
Effacer les filtres

Can I use a variable name to make logical comparison?

10 vues (au cours des 30 derniers jours)
Snr Jhay
Snr Jhay le 15 Nov 2021
Commenté : Snr Jhay le 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
  4 commentaires
Image Analyst
Image Analyst le 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.
By the way, see my answer below, and the 2 other answers.
Snr Jhay
Snr Jhay le 16 Nov 2021
Okay. I'm getting that.
Thanks a lot

Connectez-vous pour commenter.

Réponses (3)

Chunru
Chunru le 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
  5 commentaires
Stephen23
Stephen23 le 15 Nov 2021
Modifié(e) : Stephen23 le 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.
Snr Jhay
Snr Jhay le 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

Connectez-vous pour commenter.


Rik
Rik le 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
  3 commentaires
Rik
Rik le 15 Nov 2021
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?
Snr Jhay
Snr Jhay le 16 Nov 2021
They store the numbers of days in the specified month.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 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
  7 commentaires
Stephen23
Stephen23 le 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.
Snr Jhay
Snr Jhay le 19 Nov 2021
Okay. I will use the link you just sent to further enhance my studies.
Thank you very much

Connectez-vous pour commenter.

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by