"Function" & "fprintf" Help Please (thanks in advance for everyone's time)

This code works as it is, however the problem statement wanted me to write a function (not a simple script file) and to use the fprintf command to output the values.
My question is, why does this function work even if I commment out the entire function?
Also, how in the world do I implement the command "fprinf"?
Any help would be much appreciated!
% Measures velocity of derby car
% Problem Statement: This function analyzes the data of a pine wood derby car
%
function[totalavgv,maxavgv,minavgv]=stowers_colbi_PA1(timeinterval,velocitydata);
timeinterval=input('Enter Time Intervals as a row vector in seconds[a b c]: ');
velocitydata =input('Enter Velocity Data as a two dimensional array (matrix) in inches per second [a b c; d e f]: ');
avgv = mean(velocitydata);
totalavgv=mean(avgv);
maxavgv= max(avgv);
minavgv= min(avgv) ;
plot(avgv,timeinterval,'+');
grid on
title('Average Velocity as Function of Time');
xlabel('Averae Velocity [in/sec]');
ylabel('Time Intervals [sec]');
end

 Accepted Answer

It works because whatever you pass in is ignored - it asks you anyway. So the function statement can be ignored and it's basically a script.
For fprintf:
fprintf('totalavgv = %f\n', totalavgv);
Same for the other variables.

3 Comments

Thank you for your response. So when you say whatever is passed in is ignored I'm assuming that's because of the input command below it. Is that okay that it does this? Is it still considered a function and not a script?
That's correct. Normally you'd check the value of nargin. If nargin == 0 then you'd call input(), but otherwise you'd just use the values that were passed in.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!