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.

댓글 수: 4

per isakson
per isakson 2017년 8월 19일
편집: per isakson 2017년 8월 19일
Stephen23
Stephen23 2017년 8월 19일
편집: Stephen23 2017년 8월 19일
sixj=[8,16.5]
Do NOT put meta-data (e.g. the month and day) into the variable names! Accessing meta-data in variable names is fragile, complicated, buggy, hard-to-debug, obfuscated, inefficient code. Read this to know why it is a bad idea to put meta-data into variable names:
Not only is it very fragile to put meta-data into variable names, accessing any variable names dynamically is slow, buggy, and highly inefficient:
You would be much better off putting all of your data into one array, and then using indexing. For example:
data = [2017,7,6,8,16.5];
Note how this can then easily be a matrix of all of your data. Working with matrices is highly efficient in MATLAB. You could also use a structure (which could be a non-scalar structure):
data.date = [2017,7,6];
data.start = 8;
data.end = 16.5;
Either of these will be much easier to work with that using ugly and buggy dynamic variable names.
PS: the name MATLAB comes from "MATrix LABoratory", and not from "Lets split the data up into thousands of separate variables and make our code complex, ugly, and slow": when you keep your data in matrices or arrays then you can write very simple, neat, and efficient MATLAB code.
Kevin Syc
Kevin Syc 2017년 8월 19일
This program is only intended to work from 6th of july until 8th of september.
per isakson
per isakson 2017년 8월 19일
편집: per isakson 2017년 8월 19일
  • Why Matlab rather than a spreadsheet?

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2017년 8월 19일
편집: Stephen23 2017년 8월 19일

0 개 추천

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.

댓글 수: 2

Kevin Syc
Kevin Syc 2017년 8월 20일
thank you very much. This way is far better :)
Stephen23
Stephen23 2017년 8월 20일
@Kevin Syc: Indeed it is! Come and ask any time you want more help :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 8월 19일

댓글:

2017년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by