Extract Values from matrix and use them to calculate days passed

4 vues (au cours des 30 derniers jours)
Travis
Travis le 16 Juil 2013
I have to extract values from one string of input, and using those values to calculates the days passed
Here's the main code
clear,clc
%1. Get Inputs
intDate = input('Enter Date in yyyy/mm/dd format: ','s');
intYear = intDate(1:4);
intDay = intDate(9:10);
intMonth = intDate(6:7);
if isLeapYear(intYear)
intMonth = intDate(6:7);
if ~isValidMonth(intMonth)
msgbox('Pleaes enter a valid Month');
return
end
if ~isValidDate(intDay,intMonth,intYear)
msgbox('Pleaes enter a valid Day');
return
end
if intMonth==1||intMonth==3
intDaysPassed = ((((intMonth-1)*30)+intDay)-1);
elseif intMonth==2||intMonth==4||inthMonth==5||intMonth==8
intDaysPassed = (((intMonth-1)*30)+intDay);
elseif intMonth==6||intMonth==7||intMonth==9||intMonth==10
intDaysPassed = (((intMonth-1)*30)+(intDay+1));
elseif intMonth==11||intMonth==12
intDaysPassed = (((intMonth-1)*30)+(intDay+2));
end
else
strMessage = 'Please enter a valid leap year';
msgbox(strMessage)
return
end
*
Here's the code for the month
function Month = isValidMonth(intMonth)
a = (intMonth <=12);
Month = (a~=0);
end
****
Here's the code for the day
function Day = isValidDate(intDay,intMonth,intYear)
DaysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31]; % A 1x12 array
if isLeapYear(intYear)
DaysInMonth(2) = 29;
end
Day = (intDay >= 1) && (intDay <= DaysInMonth(intMonth));
end
*
Here's the code for the Year
function LeapYear = isLeapYear(intYear)
a = mod(intYear,400);
b = mod(intYear,100);
c = mod(intYear,4);
LeapYear = ((a==0)||(b~=0 && c==0));
end
Everytime I try to run it, it keeps giving me these errors
Ex. I would enter 2012/03/15 as the input
Operands to the and && operators must be convertible to logical scalar values.
Error in isLeapYear (line 5) LeapYear = ((a==0)||(b~=0 && c==0));
Error in Homework6_Question2 (line 9) if isLeapYear(intYear)
How do I convert the matrix values to scalar values so I could apply the calculation for the days passed? Is there anything else wrong in my codes? Please help !
Thank you so much !

Réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 16 Juil 2013
Look at matlab's datestr, datenum and datevec functions instead.
  3 commentaires
Iain
Iain le 16 Juil 2013
str2double is what I reckon you're missing.
str = '1999';
val = 1999;
number = str2double(str);
number == val %returns 1 if number equals val.
Travis
Travis le 16 Juil 2013
Thank you ! Thank you ! You're awesome ! It works perfectly !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Satellite Mission Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by