Search code examples
matlabdatetimematlab-figure

MATLAB Graph Functions


I have a set of data wherein I need to plot them using MATLAB. I need to use plot and scatter in one chart such that both line and scatter plot will use the same x and y axis. The x axis is datetime while y axis data will use price data. Below is the code for my data.

Y = [2015]
M = [01;02;03;04;05;06;07;08;09;10;11;12]
D = [4]

t = datetime(Y,M,D)
TA1 = [23 24 28 29 30 32 36 39 52 43 46 59]
TA2 = [25 23 27 28 29 31 31 39 52 53 46 34]
TA3 = [23 NA NA 29 NA NA 36 NA NA 43 NA NA]
TA4 = [25 NA NA 28 NA NA 31 NA NA 53 NA NA]

Using the above data, I need to plot TA1 and TA2 as line graph then embed TA3 and TA4 as scatter graph. t is their x-axis. So basically, what I am aiming is to have one plot combining TA1 and TA2 as line plot and TA3 and TA4 as scatter plot.

Please help.

Many thanks.


Solution

  • Use this:

    plot(t,TA1,t,TA2)
    hold
    scatter(datenum(t),TA3)
    scatter(datenum(t),TA4)
    

    datenum converts the dates values to numbers, and hold let you use the same axis for both plots. and good luck with your homework ;)