Checking Matlab outputs - 2 functions coded

2 views (last 30 days)
atan
atan on 11 Mar 2019
Answered: MAZEN ALHARBI on 4 Apr 2022
I'm a bit new to Matlab. I have a .m file called ex1.m which has the following code;
%% ======================= 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;
The plotData functions lives in the plotData.m file and is as follows;
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);
plot(x,y,'rx','MarkerSize',10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); %Set the x axis label
% ============================================================
end
As you see the data gets loaded in ex1.m file and ex1.m calls plotData where the data gets plotted
I want to test the plotData function and its output. Where do I test it? I cannot test in command window of plotData as it throws an error.
  3 Comments
atan
atan on 11 Mar 2019
I ahve defined X and y in ex1.m file as follows
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
Now, in plotData.m I have defined
plot(x,y,'rx','MarkerSize',10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); %Set the x axis label
Can I call plotData from the command window of ex1.m
Adam Danz
Adam Danz on 11 Mar 2019
The best investment of your time would be learning how to use debug mode. It's quite intuitive. You can put a break in your code just before the plot is created and test anything you want from the command window using the variable in your code.

Sign in to comment.

Answers (2)

Kalpavrikshika Selvakumar
Kalpavrikshika Selvakumar on 30 Dec 2019
Hi,
I presume you want to see the graph with the red cross hatches? The code looks fine.
How're you debugging it? As for me, I'm basically saying 'run ex1.m' on the octave cli (on linux terminal). It should pop up a graph on a seperate windown.

MAZEN ALHARBI
MAZEN ALHARBI on 4 Apr 2022
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);
plot(x,y,'rx','MarkerSize',10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); %Set the x axis label
% ============================================================
end

Community Treasure Hunt

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

Start Hunting!