Please help fix the syntax for my plot.

1 次查看(过去 30 天)
Karl
Karl 2022-11-10
评论: Torsten 2022-11-11
% this is a matrix sorting stocks
% first row is month, 2nd is day, 3rd is year, 4th is how much the price opened for that day, 5th is the high for that day, 6th is the low for that day, and 7th is what the price closed on that day.
Example = [ 5 27 2022 820 827 785 820;
5 28 2022 813 824 801 805;
5 29 2022 808 835 804 822;
5 30 2022 809 835 801 812;
5 31 2022 811 834 802 823;
6 1 2022 858 899 854 898;
6 2 2022 894 908 871 881;
6 3 2022 888 897 880 882;
6 4 2022 889 895 858 864;
6 5 2022 877 886 866 885]
plot(Example(:,5),Example(:,6)) %
title('Stock Highs/Lows')
xlabel('Open Days for 2022')
ylabel('High/Loss in Dollars')
xlim([0 10])
[MIN, mL] = min(Example(:,6)); %
[MAX, ML] = max(Example(:,5)); %
ylim([(MIN-100) (MAX+100)]) %
Hello all, I am trying to learn how to plot column 5 and column 6 of this matrix. Any help fixing my code is appreciated! I believe the lines where I marked a % are the areas where my writing is off. Right now I don't see anything from the graph.
  2 个评论
Torsten
Torsten 2022-11-11
I think you want to plot the columns and not the rows of the matrix.
Karl
Karl 2022-11-11
That's right my bad, I went and edited it.

请先登录,再进行评论。

回答(2 个)

Voss
Voss 2022-11-11
编辑:Voss 2022-11-11
The XData of the line you plot is Example(:,5), which is in the 800-900 range, but then you set the XLim of the axes to [0 10]. That's why nothing shows up - there's no data in that x-range.
Example = [ 5 27 2022 820 827 785 820;
5 28 2022 813 824 801 805;
5 29 2022 808 835 804 822;
5 30 2022 809 835 801 812;
5 31 2022 811 834 802 823;
6 1 2022 858 899 854 898;
6 2 2022 894 908 871 881;
6 3 2022 888 897 880 882;
6 4 2022 889 895 858 864;
6 5 2022 877 886 866 885];
plot(Example(:,5),Example(:,6)) %
title('Stock Highs/Lows')
xlabel('Open Days for 2022')
ylabel('High/Loss in Dollars')
% xlim([0 10])
[MIN, mL] = min(Example(6,:)); %
[MAX, ML] = max(Example(5,:)); %
ylim([(MIN-100) (MAX+100)]) %
Rather than plotting lows (x) vs highs (y), you probably mean to do something along these lines:
plot(Example(:,5));
hold on
plot(Example(:,6));
  2 个评论
Karl
Karl 2022-11-11
I see, how do I make the x axis the length of the how many values are on the column? I tried:
plot(length(Example),Example(:,5))
hold on
plot(length(Example),Example(:,6))
hold off
I'm still not seeing anything.
Torsten
Torsten 2022-11-11
The number of rows of a matrix is
size(Example,1)
Thus
plot(1:size(Example,1),Example(:,5:6))
If you have such basic MATLAB questions, I think you should take an online course here:

请先登录,再进行评论。


Torsten
Torsten 2022-11-11
编辑:Torsten 2022-11-11
Example = [ 5 27 2022 820 827 785 820;
5 28 2022 813 824 801 805;
5 29 2022 808 835 804 822;
5 30 2022 809 835 801 812;
5 31 2022 811 834 802 823;
6 1 2022 858 899 854 898;
6 2 2022 894 908 871 881;
6 3 2022 888 897 880 882;
6 4 2022 889 895 858 864;
6 5 2022 877 886 866 885];
hold on
plot(1:10,Example(:,5:6))
hold off
title('Stock Highs/Lows')
xlabel('Open Days for 2022')
ylabel('Highs/Lows in Dollar')
xlim([0 10])
[MIN, mL] = min(Example(:,5:6),[],'all'); %
[MAX, ML] = max(Example(:,5:6),[],'all'); %
ylim([(MIN-100) (MAX+100)]) %
grid on

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by