MATLAB: How to list through words in for loop.

conditionsloop

Hello guys
I am trying to make a program that counts hours that i soend in work. I write them in following format:
sixj=[8,16.5]
Which means that on sixth of july I worked from 8 in the morning until 4:30pm. I need to count all days and apply conditions: 1. If it is after 17:00 the pay is +33% and if it is sunday, the pay is +45%. I do not want to write code for every individual day, rather i would like to use for loop to list through days and calculate it in the loop. Does anybody have any idea how I might be able to do it? Thank you very much.

Best Answer

When you put your data into one array then your calculations will be trivially easy. For example:
M = [...
2017,07,06,08,16.5;...
2017,07,07,09,12.0;...
2017,07,08,10,18.7;...
2017,07,09,07,16.2;...
2017,07,10,07,19.9];
% [yyyy,mm,dd,beg,end]
>> hours = diff(M(:,4:5),1,2)
hours =
8.5
3
8.7
9.2
12.9
>> dtnum = datenum(M(:,1:3));
>> days = weekday(dtnum);
>> issun = days==1 % detect sundays
issun =
0
0
0
1
0
>> pay = 99;
>> hours.*~issun*pay % normal rate
ans =
841.5
297
861.3
0
1277.1
>> hours.*issun*pay*1.45 % sunday rate
ans =
0
0
0
1320.7
0
and we can add plots very simply too:
>> bar(vec,hours)
>> datetick('x','mmmdd')
etc, etc. Did you see what I did there? By making the data much simpler (using one numeric matrix), I made your difficult problem disappear entirely and wrote more working code is less time than it took you to write your question! How? By keeping the data together.