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
plot(x, y, 'rx');
(using the debugger). Try executing:x = -pi:0.1:pi;y = sin(x);plotData(x, y);
just for making surex
andy
are valid. - Rotemx = -pi:0.1:pi;y = sin(x);plotData(x, y);
at line 19 beforeplot(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 NiedererplotData
? 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 definex
andy
in your workspace (or in another function), and then only you can callplotData(x, y)
from there (the workspace or the function where you definedx
andy
). - Hoki