Creating transparent rectangles when x values are times with dates

I understand how to use the patch function to create a shaded transparent rectangle when the x and y values are just numbers. For example, the code
plot([1 2],[2 1])
x = [1.25 1.5 1.5 1.25];
y = [1 1 1.75 1.75];
p=patch(x,y,'r');
set(p,'FaceAlpha',0.5);
creates a graph with the plotted line and a transparent red rectangle. My actual plot has the x values (which are a time and date) exported from excel, and then converted to matlab date and time by using datetime and stored in the vector Time. After plotting my data, I tried to do something like this to add the shaded rectangle:
x = [Time(1) Time(2) Time(2) Time(1)];
y = [30 30 35 35];
p=patch(x,y,'r');
set(p,'FaceAlpha',0.5);
This gives me the error: "Error using patch. Vectors must be the same length." The vectors x and y are both length 4, so I don't understand why I get this error. Since it worked in the simple example, I am assuming it has something to do with the x values being in the datetime form.

5 commentaires

Rosalie - can you give an example of your Time vector? How have you created it, what are the dimensions, etc.? Also, what do the following return
class(x)
and
size(x)
This is how I created the Time vector:
Time = xlsread('timesheet.xlsx', sheet, 'E:E'); %load log from excel file
Time = datetime(Time,'ConvertFrom','excel'); %converts to matlab date and time
The cells of the column in the excel file that it imports are in the format month/day/year time. For example: 10/9/2015 2:37:00 PM. After converting using datetime, it is in the form 09-Oct-2015 14:37:00.
The value in the workspace for Time is 68x1 datetime.
for class(x):
ans =
datetime
For size(x):
ans =
1 4
Rosalie - according to patch, the x input array can only be of type single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64. In your case, you are using a datetime data type. You may need to convert that time vector to a numeric equivalent.
James
James le 18 Jan 2017
Modifié(e) : James le 18 Jan 2017
Digging some into the source code for stairs, it looks like there's an undocumented function to do this: matlab.graphics.internal.makeNumeric.
[XData, YData] = matlab.graphics.internal.makeNumeric(axis_handle, XData, YData);
Like all undocumented/internal functionality, your mileage may vary and it's subject to change at any time.
The documented function to use is ruler2num.

Connectez-vous pour commenter.

Réponses (2)

James Uber
James Uber le 20 Jan 2018
I had the same problem using patch. fill() has no such limitation with date time axes, so perhaps that will work for you as well.

1 commentaire

Thanks, fill works much better than rectangle as it supports datetime values.

Connectez-vous pour commenter.

Please try to convert the time to a numerical format:
TimeNum = datenum(Time);
Or create it directly with x2mdate
TimeNum = x2mdate(xlsread('timesheet.xlsx', sheet, 'E:E'));
Then plotting should work. To get rid of the ugly axis lables, use datetick.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by