time intervals distribution in 24 hours day time
Show older comments
Start and end time for off-peak hour is from 00.00 to 07.00 and 21.00 to 24.00. The power usage price at this time interval is 03.00 cents. Start and end time for mid-peak hour is from 11.00 to 17.00. The power usage price at this time interval is 05.00 cents. Start and end time for peak hour is from 07.00 to 11.00 and 17.00 to 21.00. The power usage price at this time interval is 07.00 cents.
I want to code that intervals . Please help me how to do it.

4 Comments
dpb
on 23 Jun 2019
Looks like homework...show your work and ask specific MATLAB question where you got stuck.
Muhammad Shahroz Khan
on 24 Jun 2019
dpb
on 24 Jun 2019
doc duration
Muhammad Shahroz Khan
on 24 Jun 2019
Answers (1)
Pullak Barik
on 24 Jun 2019
Edited: Pullak Barik
on 24 Jun 2019
I guess that making a table that stores these times will help. I have attached a code below, please tell if it helps-
tariff = table('Size', [0 4], 'VariableTypes', {'duration', 'duration', 'string', 'double'});
tariff.Properties.VariableNames = {'Start_Time', 'End_Time', 'Tariff_Name', 'Cost'};
tariff = [tariff; {duration('00:00', 'InputFormat', 'hh:mm'), duration('07:00', 'InputFormat', 'hh:mm'), 'Off-peak', 3.0}];
tariff = [tariff; {duration('07:00', 'InputFormat', 'hh:mm'), duration('11:00', 'InputFormat', 'hh:mm'), 'Peak', 7.0}];
tariff = [tariff; {duration('11:00', 'InputFormat', 'hh:mm'), duration('17:00', 'InputFormat', 'hh:mm'), 'Mid-peak', 5.0}];
tariff = [tariff; {duration('17:00', 'InputFormat', 'hh:mm'), duration('21:00', 'InputFormat', 'hh:mm'), 'Peak', 7.0}];
tariff = [tariff; {duration('21:00', 'InputFormat', 'hh:mm'), duration('00:00', 'InputFormat', 'hh:mm'), 'Off-peak', 3.0}];
This code stores your intervals in the form of a table, as shown below-
.png)
9 Comments
Muhammad Shahroz Khan
on 24 Jun 2019
dpb
on 24 Jun 2019
Aw, come on...at least try something on your own! Being spoon-fed is no way to learn. :(
Try
tariff.Tariff=categorical(tariff.Tariff_Name); % make a categorical variable of classes
rate=tariff.Cost(tariff.Tariff=="Peak")
Muhammad Shahroz Khan
on 24 Jun 2019
dpb
on 24 Jun 2019
Well, it presumes you followed the previous poster's example to create the table first.
Pullak Barik
on 25 Jun 2019
I guess trying
tariff.Tariff_Name = categorical(tariff.Tariff_Name);
will be better. Then, by writing code that is similar to the one posted by dpb for finding 'rate', you can use Tariff_Name categorical to store Start_Time and/or End_Time in separate variables.
dpb
on 25 Jun 2019
If you want to overwirte the original Name variable -- which I chose to create second, shorter name for the categorical.
The error, however, is that the table tariff itself doesn't yet exist in his workspace.
Pullak Barik
on 25 Jun 2019
Ya, that actually does seem to be the issue.
dpb
on 25 Jun 2019
[MSK Answer moved to Comment -- dpb]
I totally understand that but kindly tell me how to remove this error
dpb
on 25 Jun 2019
Follow Pullak's example to create the table first. (NB: depending on the release you're using, you may have to revise his syntax--w/ R2017b here I had to do a fair amount of retrofitting as various features he used weren't yet implemented --starting with duration() not yet accepting a string date as input.
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!