How to list through words in for loop.
Show older comments
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 Comments
per isakson
on 19 Aug 2017
Edited: per isakson
on 19 Aug 2017
- What about the sixth of June?
- Regulars here oppose against squeezing data into names of variables
- Timetables, Time-stamped data in tabular form might be worth checking out
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
on 19 Aug 2017
per isakson
on 19 Aug 2017
Edited: per isakson
on 19 Aug 2017
- Why Matlab rather than a spreadsheet?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!