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 个评论
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
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.
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!