plotData does not plot anything

I'm trying to plot a set of points in Matlab using plotData function, but the figure window is empty. Moreover, the plot function works correctly, I only have issues with the plotData function. Here is my code:
x = [1;2;3;4;5]
y = x.^2
plotData(x,y)
MATLAB Version: 9.1.0.441655 (R2016b) Operating System: Mac OS X Version: 10.10.5 Build: 14F27

3 commentaires

Stephen23
Stephen23 le 6 Jan 2017
Modifié(e) : Stephen23 le 6 Jan 2017
The online documentation search cannot find any reference to "plotData":
Nor can "plotData" be found in the list of MATLAB functions:
Can you please show a documentation reference, or anything that shows that this is a MATLAB function.
Ivan Panshin
Ivan Panshin le 6 Jan 2017
Hm, interesting. I found this function in the files for my homework assignment from machine learning class. It said that I can use either matlab of octave. And yeah, I can't find this function in the documentation as well.
Ujval Madhu
Ujval Madhu le 4 Nov 2021
@Ivan Panshin it seems the function to use is plot(x,y) for plotting rather than plotData - which is a function we create in the homework which makes use of 'plot' method to get the 2d-plot of the data.

Connectez-vous pour commenter.

Réponses (4)

Geoff Hayes
Geoff Hayes le 6 Jan 2017

2 votes

Ivan - if you are using the plotData function from https://github.com/zhouxc/Stanford-Machine-Learning-Course/blob/master/Logistic%20Regression/mlclass-ex2/plotData.m, then I observe the following errors when running your code
Index exceeds matrix dimensions.
Error in plotData (line 14)
plot(X(pos , 1) , X(pos , 2) , 'k+' , 'LineWidth' , 2 , 'MarkerSize' , 7);
The function seems to assume that the second input parameter y consists of ones and zeros for positive and negative elements of x. Also, the code assumes that x is a Mx2 matrix.

3 commentaires

No, unfortunately, it doesn't seem that way. I have attached the files that they provided me + one example of how they use this function (you can find it in ex1.m file)
%%======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
% Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y);
fprintf('Program paused. Press enter to continue.\n');
pause;
Geoff Hayes
Geoff Hayes le 6 Jan 2017
Ivan - please attach the relevant code. I'm reluctant to open a zip file. :)
haoran wan
haoran wan le 29 Sep 2020
Use plot replace plotData, Because this is matlab rather than Octave

Connectez-vous pour commenter.

Anthony Garber
Anthony Garber le 2 Fév 2018
Modifié(e) : Anthony Garber le 2 Fév 2018
Just in case anyone stumbles across this in the future... plotData is a function created in a .m file included in the classes homework download. It simply opens a figure:
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit.
figure; % open a new figure window
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10);
% ============================================================
end

2 commentaires

Andrea Sbaragli
Andrea Sbaragli le 21 Sep 2020
same problem... X unrecognized..
His code said
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
% Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y);
So it couldn't say X was unrecognized. If data is there, then it would have X. If data were not available then it would complain about data, not X.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 6 Jan 2017

0 votes

Try plot() instead of plotData:
plot(x, y, 'k+-' , 'LineWidth' , 2 , 'MarkerSize' , 7);
grid on;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
N/A
N/A le 10 Oct 2019

0 votes

From the comments, I understand that 'plotData.m' intends to plot the classification data where X indicates the location of a point and y corresponds to the classification of the said point (either 0 or 1). One can then use a simple code (attached herein) to separate the two classes and then plot them individually.
Hope it helps...
X_pos=X(y==1,:);
X_neg=X(y==0,:);
plot(X_pos(:,1),X_pos(:,2),'k+');
plot(X_neg(:,1),X_neg(:,2),'ko');

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by