How to list through words in for loop.

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 comentarios

per isakson
per isakson el 19 de Ag. de 2017
Editada: per isakson el 19 de Ag. de 2017
Stephen23
Stephen23 el 19 de Ag. de 2017
Editada: Stephen23 el 19 de Ag. de 2017
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 el 19 de Ag. de 2017
This program is only intended to work from 6th of july until 8th of september.
per isakson
per isakson el 19 de Ag. de 2017
Editada: per isakson el 19 de Ag. de 2017
  • Why Matlab rather than a spreadsheet?

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 19 de Ag. de 2017
Editada: Stephen23 el 19 de Ag. de 2017
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 comentarios

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Ag. de 2017

Comentada:

el 20 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by