Checking Matlab outputs - 2 functions coded

1 次查看(过去 30 天)
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 个评论
atan
atan 2019-3-11
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 2019-3-11
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.

请先登录,再进行评论。

回答(2 个)

Kalpavrikshika Selvakumar
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 2022-4-4
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

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by