"ans" variable function output unwanted
54 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Giuseppe Naselli
2014-1-12
1 个评论
Image Analyst
2014-1-12
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".
更多回答(2 个)
Jan
2014-1-11
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);
0 个评论
Image Analyst
2014-1-11
Put a semicolon at the end of the line to suppress display of the ans output to the command window.
2 个评论
Image Analyst
2014-1-11
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/
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!