"ans" variable function output unwanted

56 views (last 30 days)
Hi All,
I wrote a function in matlab in order to process some data. The function works fine and the results are as I want.
However, the function generates (unexpectatly) a variable "ans" which correspond to the first output of the function (time)
See my code below
I know that by default when a function is created all the first output value is returned. How can I avoid that (in order to leave clear the workspace)?
Thanks all for you help
G
%%Time Domain and Logging Frequency
time = Data_Raw(:,1)/1000; % 1st Colum is logging time in ms (from Arduino)
Fs = 1/mean(diff(time)); % Actual sample frequency (Hz)
TO_USER = ['The Real Logging Time of the system is in average ', num2str(Fs), ' Hz'];
clc
disp(TO_USER)
clear TO_USER
%%From Analog to Acceleration in g
ax = 3.5/(1023/2) * Data_Raw(:,2) - 3.5; % Accelerometer range -3.5g +3.5g
ay = 3.5/(1023/2) * Data_Raw(:,3) - 3.5; % Accelerometer range -3.5g +3.5g
az = 3.5/(1023/2) * Data_Raw(:,4) - 3.5; % Accelerometer range -3.5g +3.5g
%%Smooting the Accelerations
Ax = feval(fit(time, ax, 'smoothingspline'), time); % with parameter set to Auto
Ay = feval(fit(time, ay, 'smoothingspline'), time);
Az = feval(fit(time, az, 'smoothingspline'), time);
clear ax ay az;
%%HIGH PASS FILTER to compensate for the iniail position of the accelerometer (not always in level)
% filter definition
%Fc = 0.2; % cutoff frequency (Hz)
%[B,A]=butter(5,Fc/(Fs/2),'high'); % calcultate the filter numerator and denominator coeffients
%Ax_0 = filtfilt(B,A,Ax); % apply the filter defined in the lines above
%Ay_0 = filtfilt(B,A,Ay);
%Az_0 = filtfilt(B,A,Az);
%clear A; clear B; clear Fc; clear Fs;
% THE HIGH PASS FILTER MODIFY THE DATA TOO MUCH - THIS METHOD DOES NOT
% SEEMS APPROPRIATE, then the solution is zeroing taking the average off
%%Zeroing by taking off the average
Ax_zeroed = Ax - mean(Ax);
Ay_zeroed = Ay - mean(Ay);
Az_zeroed = Az - mean(Az);
%%Plotting
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'XTickLabel',{'','','','','',''},'Position',[0.11843187660668 0.682981090100111 0.774884318766067 0.309749136467419]);
box(axes1,'on');
hold(axes1,'all');
% Create plot
plot(time,Ax_zeroed,'Parent',axes1,'DisplayName','Longitudinal g');
% Create axes
axes2 = axes('Parent',figure1,'XTickLabel',{'','','','','',''},'Position',[0.118431876606679 0.364756889268502 0.774884318766067 0.309749136467419]);
box(axes2,'on');
hold(axes2,'all');
% Create plot
plot(time,Ay_zeroed,'Parent',axes2,'Color',[0 0.5 0],'DisplayName','Lateral g');
% Create ylabel
ylabel('Acceleration (g)','FontSize',16);
% Create axes
axes3 = axes('Parent',figure1,'XTick',[0 50 100 150 200 250],'Position',[0.118431876606679 0.0452654481856993 0.774884318766067 0.309749136467419]);
box(axes3,'on');
hold(axes3,'all');
% Create plot
plot(time,Az_zeroed,'Parent',axes3,'Color',[1 0 0],'DisplayName','Vertical g');
% Create xlabel
xlabel('Time (sec)','FontSize',14);
% Create legend
legend(axes1,'show');
% Create legend
legend(axes2,'show');
% Create legend
legend(axes3,'show');
%%Saving
% TO BE COMPLETED
end

Accepted Answer

Giuseppe Naselli
Giuseppe Naselli on 12 Jan 2014
Found the problem and it is very stupid (and embarassing)
The function is absolutely fine. I called the function with the command
[time,Ax,Ay,Az] = data_acq(varname)
but as any other function in Matlab, if I do not use the ";" like
[time,Ax,Ay,Az] = data_acq(varname);
Matlab automatically provides the first output of the function.
SOrry for populating this form with this stupid questions
  1 Comment
Image Analyst
Image Analyst on 12 Jan 2014
As my answer said (which you should have accepted instead of this one). Though I'm still confused because what you said is not true: "as any other function in Matlab, if I do not use the ";" Matlab automatically provides the first output of the function." Not true - it reports all of them to the command window, not just the first one. Look:
function test3
[a,b,c] = f()
function [a,b,c] = f()
a=1;
b=2;
c=3;
In the command window you see
>> test3
a =
1
b =
2
c =
3
Moreover, the variables are given their actual names, not the generic "ans" so that's why I'm puzzled by your "Answer".

Sign in to comment.

More Answers (2)

Jan
Jan on 11 Jan 2014
What exactly is the problem? That the command window contains some output or that the variable ans is created?
  • For the first case, set a semicolon behind the command, which calls the function. Example:
sin(2) % ans = 0.9093 appears
sin(2); % nothing appears
  • For the 2nd case, you can and should avoid to create an output, if you do not want to obtain it in the caller:
function NoOutput(in1)
your operations here...
If your function should reply nothing only if no outputs are wanted from the caller:
function Out = OutputOnDemandOnly(In)
Reply = sin(In);
if nargout ~= 0
Out = Reply;
end
Now try:
OutputOnDemandOnly(2)
OutputOnDemandOnly(2);
a = OutputOnDemandOnly(2)
a = OutputOnDemandOnly(2);

Image Analyst
Image Analyst on 11 Jan 2014
Put a semicolon at the end of the line to suppress display of the ans output to the command window.
  2 Comments
Giuseppe Naselli
Giuseppe Naselli on 11 Jan 2014
Hi and thanks for your answer.
in which line you mean?
I think I have already a semicolon everywhere there is a command
G
Image Analyst
Image Analyst on 11 Jan 2014
Step through your code one line at a time in the debugger to see where it gets produced. http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!