Problem while plotting twice in a figure

조회 수: 2 (최근 30일)
Ramo Rafsel
Ramo Rafsel 2020년 8월 11일
댓글: Shae Morgan 2020년 8월 14일
I have 2 imported columns from excel (time and value), that contain more than 10000 rows, I plotted them and also the average of the whole values.
Now I want to plot both the time on the x axis and the values on the y axis with the average line (see second code) in one figure, I get the following error:
Values plotted against x-axis must be categorical values. To create categorical values, use the categorical function.
when I convert the value to a categorical one, it says: " Invalid data type. First argument must be numeric or logical."
Can Anyone tell me where the problem can be?
%the first correct code:
nexttile
plot(time,value)
nexttile
plot([0 size(value,1)],[avg avg], 'y-', 'LineWidth',2);
%The second one where I get the error:
nexttile
hold on
plot(time,value)
plot( [0 size(value,1)],[avg avg], 'y-', 'LineWidth',2);
hold off
  댓글 수: 2
KSSV
KSSV 2020년 8월 11일
Wjat is avg? How did you find it? Showus the full code.
Ramo Rafsel
Ramo Rafsel 2020년 8월 11일
편집: Ramo Rafsel 2020년 8월 11일
I forgot to mention that avg is the average of the "value" column and it was calculated as the following:
avg5 = sum(value)/size(value,1);
the time and value are the imported table columns from excel.

댓글을 달려면 로그인하십시오.

채택된 답변

Shae Morgan
Shae Morgan 2020년 8월 13일
Try this out: Since I didn't have your variable class, I had to assume you're dealing with time-stamps here. This works for those:
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing
  댓글 수: 2
Shae Morgan
Shae Morgan 2020년 8월 13일
Shae Morgan
Shae Morgan 2020년 8월 14일
If this answers your question, please accept the answer. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by