필터 지우기
필터 지우기

Checking Matlab outputs - 2 functions coded

조회 수: 1 (최근 30일)
atan
atan 2019년 3월 11일
답변: MAZEN ALHARBI 2022년 4월 4일
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
Kalpavrikshika Selvakumar 2019년 12월 30일
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