My figure wont display a line

4 ビュー (過去 30 日間)
Hasan Swain
Hasan Swain 2020 年 4 月 16 日
コメント済み: Hasan Swain 2020 年 4 月 17 日
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')

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 4 月 17 日
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 件のコメント
Hasan Swain
Hasan Swain 2020 年 4 月 17 日
Thanks a lot Geoff! This was very helpful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by