My plot comes up as a white graph with no line.

28 ビュー (過去 30 日間)
Yunus Mercan
Yunus Mercan 2021 年 4 月 12 日
コメント済み: Image Analyst 2021 年 4 月 12 日
When I try to plot with this code which is given below, nothing comes up. There are no errors, there's just a blank page. I want to lines between y values depending t values.
Please help me. Thank you from now.
clear all;
clc;
for t=1:25
if t==1,2,3,4,5;
y(t) = 1
elseif t==6,7,8,9,10;
y(t) = 0.9
elseif t==11,12,13,14,15,;
y(t) = 1
elseif t==16,17,18,19,20,;
y(t) = 1.05
elseif t==21,22,23,24,25,;
y(t) = 1
end
end
figure
hold on;plot(t, y)
xlabel('t(1/100)'); ylabel('VR');
title('VR Değişimi');
Expected graph is like that

採用された回答

Stephen23
Stephen23 2021 年 4 月 12 日
編集済み: Stephen23 2021 年 4 月 12 日
The basic problem is that this syntax
if t==1,2,3,4,5;
is equivalent to writing this (i.e. each expression is evaluated independently):
if t==1
2
3
4
5;
That is not the correct way to compare mulitple values at once. Remember that the name MATLAB comes from MATrix LABoratory: you should always keep your data in vectors/matrices/arrays. For example:
if any(t==[1,2,3,4,5])
% ^^^^^^^^^^^ vector!
Note that you should preallocate y before the loop:
Note that whilst it is certainly possible to plot vertical lines, your approach will not do this.
  3 件のコメント
Yunus Mercan
Yunus Mercan 2021 年 4 月 12 日
Thank you Stephen Cobeldick , it worked.
Image Analyst
Image Analyst 2021 年 4 月 12 日
Instead of an if/else, a switch statement can be used to compare multiple cases at a time. For example:
switch(result)
case 1
disp('result is 1')
case {52, 78}
disp('result is 52 or 78')
end

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

その他の回答 (2 件)

Atsushi Ueno
Atsushi Ueno 2021 年 4 月 12 日
  1. After exexuting your script, y is like [1 0 0 0 0 0.9 0 0 0 0 1 ....] it must be different from what you expected.
  2. After executinf for loop, the variable t is going to be deleted. So, you need t = 1:25 after the for loop.
How about the code below?
clear all;
clc;
t = 1:25;
y = repelem([1 0.9 1 1.05 1], 1, 5);
figure
hold on;
plot(t, y)
xlabel('t(1/100)');
ylabel('VR');
title('VR Değişimi');
  3 件のコメント
Atsushi Ueno
Atsushi Ueno 2021 年 4 月 12 日
How about using stem(t, y) or stairs(t, y) instead of plot(t, y)?
You have to care about index for using other functions.
clear all;
clc;
t = 0:24; % just changed from 1:25 for stairs()
y = repelem([1 0.9 1 1.05 1], 1, 5);
figure
hold on;
stairs(t, y);
xlabel('t(1/100)');
ylabel('VR');
title('VR Değişimi');
Yunus Mercan
Yunus Mercan 2021 年 4 月 12 日
Atsushi Ueno,It works fine this time, thank you for your interest.

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


Jithin Nambiar J
Jithin Nambiar J 2021 年 4 月 12 日
編集済み: Jithin Nambiar J 2021 年 4 月 12 日
Your code can get pretty long and tedious. The if statement in your code would not work well because it wont compare the values separated by commas properly.
t=0.0001:0.01:40;
x=[t>0]-0.05.*[t>5]+0.05.*[t>15]+0.05.*[t>25]-0.05.*[t>35];
plot(t,x)
grid on;
xlabel('t(1/100)'); ylabel('VR');
title('VR Değişimi');
Unless you plan to plot another graph in the same above plot. You don't need to use
hold on
Hope this helps. Cheers!
  1 件のコメント
Yunus Mercan
Yunus Mercan 2021 年 4 月 12 日
Thank you Jithin Nambiar J,It works fine.

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

カテゴリ

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