0
votes

I am currently taking Andrew Ng's Stanford Machine learning course and am using MATLAB to complete the programming assignments despite being new to the MATLAB programming language as the course requires completion of assignments in either Ocatave or MATLAB. For the first programming assignment the course offers Octave/MATLAB script that steps you through the exercises. I am attempting to write a MATLAB function that will plot data for x and y variables, however, am continuing to run into the same error and am beginning to spin my wheels with finding a solution. I have a plotData.m file I am working off of and here is the code I have put together:

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
hold on;

% ====================== 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);

plot(x, y, 'rx');
xlabel('population (in tens of thousands)');
ylabel('profit (in $10,000s)');

hold off;

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

end

Every time I run this script I receive an error stating 'Not enough input arguments. Error in plotData (line 19) plot(x, y, 'rx');'. This is a fairly ambiguous error message and I am not sure how to interpret what is wrong here. I don't see why I wouldn't have enough input arguments in this example as the function explicitly takes both x and y and uses each to plot data as defined by the function. Any help would be greatly appreciated.

I am using MATLAB version 9.6.0.1114505 (R2019a) Update 2

1
Your code should have worked. Place a Breakpoint at line 19, and step into plot(x, y, 'rx'); (using the debugger). Try executing: x = -pi:0.1:pi;y = sin(x);plotData(x, y); just for making sure x and y are valid. - Rotem
What you have written is not a script, it's a function. How are you calling the function? - David
@CrisLuengo Executing which plot returns 'built-in (/Applications/MATLAB_R2019a.app/toolbox/matlab/graph2d/plot)' - I have not shadowed the plot function with another file or variable - Jake Niederer
@Rotem Executing x = -pi:0.1:pi;y = sin(x);plotData(x, y); at line 19 before plot(x, y, 'rx'); ran for ~15 minutes before MATLAB crashes and quits. Furthermore, I'm not sure why the function will not run without error when I remove the variable assignment within the function. The ultimate goal is to be able to use the function at any point and define x and y at that time rather than within the function itself. I'm not sure what I'm missing here? - Jake Niederer
It is primordial to repeat @David's question: How are you calling the function plotData ? You would get this error if you try to run call the function just by pressing the green [RUN] button from the matlab menu. You need to define x and y in your workspace (or in another function), and then only you can call plotData(x, y) from there (the workspace or the function where you defined x and y). - Hoki

1 Answers

0
votes

I was getting the same error on running 'plotData.m', Please try running 'ex1.m' and not 'plotData.m'