MATLAB Answers

Plot line thickness changes based on value

50 ビュー (過去 30 日間)
Emerson Butler
Emerson Butler 2019 年 4 月 11 日
コメント済み: Emerson Butler 2019 年 4 月 11 日
I'm trying to plot some boolean values, and to represent True (1) or False (0), I'd like to change the line thickness over time. If the value is false, the line should be thin, and if the value is true the line should be thick. I've been trying various methods, and most recently I've tried following this method:
This is what I've come up with, but which doesn't work:
%Logical Values
x = [1 1 0 0 1 0 1 1 1 0 0 0];
%c = desired Line width = 1 for False and 2 for True
c = x+1;
%t-axis values
t = 1:length(x);
%Just want a straight line, not dependent on x, only the line thickness
%should be dependent on x.
z = zeros(1,length(x));
This results in an error saying c is invalid.
Another method I've tried which also doesn't work was taken from another similar question, found here: answered by Seth DeLand.
%Set Threshold
Level = 1;
%Determine indices where x is at the threshold
AtLevel = (x == Level);
%Create 2 lines of 0s
ThinLine = zeros(size(x));
ThickLine = zeros(size(x));
%Set values not to be plotted as NaN
ThinLine(AtLevel) = NaN;
ThickLine(AtLevel) = NaN;
hold on;
This doesn't give any errors, but also doesn't result in a continuous line.
This is just what I've tried, anything that will help me plot the values in the way I've described would be lovely.
Edit: I'd eventually be plotting multiple different boolean signals, and I'd like them to be on the same plot in a nice way. So one of the lines would be at 0, one at 1, one at 2, and so on. I'd probably do this by making the line as ones(size(x))*n, where n is some integer. That way I can visually represent a bunch of boolean signals as they change over time.


Mark Sherstan
Mark Sherstan 2019 年 4 月 11 日
Here is one way of doing it:
%Logical Values
x = [1 1 0 0 1 0 1 1 1 0 0 0];
t = 1:length(x);
% Create figure and line thickness
hold on
thick = 1
% Loop through logicals
for ii = 2:length(x)
if x(ii) == true
thick = thick + 1;
thick = thick - 1;
% Prevent line from dissapearing
if thick <= 1
thick = 1
  2 件のコメント
Emerson Butler
Emerson Butler 2019 年 4 月 11 日
Fixed the indices, so the new code is:
x = [1 1 0 0 1 0 1 1 1 0 0 0];
t = 1:length(x)+1;
z = zeros(1,length(x)+1);
hold on;
for m = 1:length(x)
if x(m) == true
Which results in:
This is exactly what I wanted. A straight line where only the thickness is dependent on the value, and if the value is true for 1, the line is thick for all of 1, if it is true for 2, the line is thick for all of 2, if the value is false for 3, then the line is thin for all of 3, and so on. (e.g. index 1 = true results in 1 <= thick < 2 ). Thanks Mark for the guidance, when I had tried for loops to plot originally I was only trying to plot one point in the for loop which was resulting in an empty graph, instead of plotting from index:index+1.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by