6

I have a simple data file looking like this:

data.txt
34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1

And I am trying to plot its data using the following code:

data = load('data.txt');
X = data(:, [1, 2]); y = data(:, 3);

plotData(X, y);

hold on;

xlabel('Exam 1 score')
ylabel('Exam 2 score')

legend('Admitted', 'Not admitted')
hold off;

pause;

However this bring me the following errors:

warning: legend: plot data is empty; setting key labels has no effect
error: legend: subscript indices must be either positive integers less than 2^31 or logicals

And nothing gets plotted.

I don't understand what's wrong. The working directory is fine in octave.

How can I fix this?

Many thanks

6
  • 1
    I think you need to use cell arrays there - legend({'Admitted', 'Not admitted'}). Commented Apr 7, 2015 at 21:34
  • I tried but its still not working :( Commented Apr 7, 2015 at 21:37
  • 1
    what do you get when printing X and y? Are they really empty? Also, try using csvread instead of load. Load is for matlab variables stored in files. Commented Apr 7, 2015 at 21:51
  • 4
    plotData is not a standard octave function, look at that. Sub plot for it and it works. Commented Apr 7, 2015 at 22:09
  • 2
    You need to implement the function plotData in plotData.m Read the document. Commented Jun 11, 2015 at 12:40

4 Answers 4

10

You are trying the assignment on week three in machine learning course by Andrew Ng on coursera. There in ex2.m file, there is a call to function plotData(X,y) which refers to the function written in plotData.m file. You may thought that plotData is a default function in octave, but you actually need to implement that function in plotData.m file. Here is my code in plotData.m file.

function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure 
%   PLOTDATA(x,y) plots the data points with + for the positive examples
%   and o for the negative examples. X is assumed to be a Mx2 matrix.

% Create New Figure
figure; hold on;

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%

pos = find(y==1);
neg = find(y==0);
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);

% =========================================================================



hold off;

end
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider this the accepted answer if this helps the most.
7

If you read the pdf carefully, the PlotData.m codes are is in the pdf. Here is the code:

% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);

Comments

3

1) X is a 5x2 array while y is a 5x1 array

2) plotData isn't a Matlab command, use plot instead

Try the following code:

data = load('data.txt');
x1 = data(:, 1);
x2 = data(:,2);
y = data(:, 3);

plot(x1, y);
hold on
plot(x2,y);

xlabel('Exam 1 score')
ylabel('Exam 2 score')

legend('Admitted', 'Not admitted')
hold off;
pause;

Comments

0
% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);

You have to type the above code (not copy and paste) from the PDF document to the plotData.m file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.