Same code in a loop but different plots

4 views (last 30 days)
Youngmin
Youngmin on 4 Nov 2022
Commented: Youngmin on 4 Nov 2022
Hello world,
I am working on making plots to visualize my data, but I am not sure what is wrong with my code. I tried to draw four subplots to display when each muscle was activated by filling the color on the area between the threshold and EMG signals. Even though I used for loop, the first column data keep displaying different patterns of filling and increasing threshold. I have attached sample data and code below.
I would really appreciate any help to find out how to fix this issue.
clear;
clc;
close all;
load sample.mat;
thr = [0.0057 0.0401 0.0090 0.0216];
t = (0:length(sample)-1)/2000;
for i = 1:4;
subplot(4,1,i);
plot(t,movmean(sample(:,i),100));
hold on;
fill(t,max(movmean(sample(:,i),100),thr(i)), ...
thr(i),"LineStyle","none");
xlim([0, t(end)]);
end
  2 Comments
KSSV
KSSV on 4 Nov 2022
I think code works fine. What you are expecting?
Youngmin
Youngmin on 4 Nov 2022
What I am expecting is that all four subplots look similar to each other. However, the threshold of the first plot keeps increasing unlike other subplots despite a single value and the filling area keeps reversing to over or to under the threshold. I have attached the screenshot of the plot (created by the code above) for a better understanding. Interestingly, this issue is gone when I use the area function, but I would like to use fill because the area does not allow "LineStyle - none'. Do you have any suggestions?

Sign in to comment.

Answers (1)

KSSV
KSSV on 4 Nov 2022
area also supports linestyle, none.
load('sample.mat')
thr = [0.0057 0.0401 0.0090 0.0216];
t = (0:length(sample)-1)/2000;
for i = 1:4
subplot(4,1,i);
plot(t,movmean(sample(:,i),100));
hold on;
area(t,max(movmean(sample(:,i),100),thr(i)), ...
thr(i),'LineStyle','none');
xlim([0, t(end)]);
end
  1 Comment
Youngmin
Youngmin on 4 Nov 2022
@KSSV Thank you for your suggestion. I would like to remove the line of threshold when the signals are not exceeding the threshold as you can see in the second plot (red solid lines are across the plot despite LineStyle, none). Is there any way to remove that line and keep the filled area only when the signal exceeds the threshold?

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!