Please help fix the syntax for my plot.

1 view (last 30 days)
Karl
Karl on 10 Nov 2022
Commented: Torsten on 11 Nov 2022
% 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 Comments
Torsten
Torsten on 11 Nov 2022
I think you want to plot the columns and not the rows of the matrix.
Karl
Karl on 11 Nov 2022
That's right my bad, I went and edited it.

Sign in to comment.

Answers (2)

Voss
Voss on 11 Nov 2022
Edited: Voss on 11 Nov 2022
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 Comments
Karl
Karl on 11 Nov 2022
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 on 11 Nov 2022
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:

Sign in to comment.


Torsten
Torsten on 11 Nov 2022
Edited: Torsten on 11 Nov 2022
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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!