My figure wont display a line

4 Ansichten (letzte 30 Tage)
Hasan Swain
Hasan Swain am 16 Apr. 2020
Kommentiert: Hasan Swain am 17 Apr. 2020
For some reason I am unable to produce a line to connect my data points on the plot shown below. Please help. Also, I am using version 2018a.
time_array = linspace(1,34,34);
plot(time_array(1:2),2,'-ob')
hold on
plot(time_array(3:32),0,'-ok')
plot(time_array(33:34),-2,'-ob')

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 17 Apr. 2020
Hasan - look closely at your calls to plot
plot(time_array(1:2),2,'-ob')
The first input is an array of two elements but the second input is an array of one element. So what is happening here? If you change the code to
hPlots = plot(time_array(1:2),2,'-ob')
then hPlots will be an array of graphics objects that are created when you call plot for this data. Now when you run this code and since there is no semi-colon at the end of the line, you will see something similar to the following in the command window
hPlots =
188.0033
189.0033
This tells us that two graphics objects are being created and so we have two separate plots! And so this is why there is no line connecting the data points. You need to have a 1-1 mapping of x and y points as your inputs. You can easily fix this by just replicating the second input for as many elements in the first input array. Try the following
hPlots = plot(time_array(1:2),repmat(2,1,length(time_array(1:2))),'-ob')
hold on
plot(time_array(3:32),repmat(0,1,length(time_array(3:32))),'-ok')
plot(time_array(33:34),repmat(-2,1,length(time_array(33:34))),'-ob')
Now your data points shoudl be connected. (Note also how hPlots is a single graphics object handle.)
  1 Kommentar
Hasan Swain
Hasan Swain am 17 Apr. 2020
Thanks a lot Geoff! This was very helpful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by