Checking Matlab outputs - 2 functions coded
Mostrar comentarios más antiguos
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 comentarios
Geoff Hayes
el 11 de Mzo. de 2019
Ajay - please clarify what you mean by I want to test the plotData function and its output. What exactly do you want to test? Do you have a set of known inputs that will produce a specific output? Yes, you can test it in the command window so long as you provide the x and y input data like
>> x = -2*pi:0.01:2*pi;
>> y = cos(x);
>> plotData(x,y);
atan
el 11 de Mzo. de 2019
Adam Danz
el 11 de Mzo. de 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.
Respuestas (2)
Kalpavrikshika Selvakumar
el 30 de Dic. de 2019
0 votos
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
el 4 de Abr. de 2022
0 votos
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
Categorías
Más información sobre Networks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!