Outputs of a function

1 view (last 30 days)
Jaden
Jaden on 26 Jun 2012
Hello! I'm currently writing functions that take input, and need to return a ouput to be used by a later function. However, in the function programming, I know that I have many different variables that I'm defining in order to do the function that I need. So when I try to get the function to do an output argument (i.e. X = function(input);), it gives me the error that I have too many output arguments.
Is there a way that I can label (inside the function itself) the one ouput I want so that it only sees that variable as the correct output?

Accepted Answer

tlawren
tlawren on 26 Jun 2012
Is your function declaration correct? For example, the following two functions are declared to return one and two variables respectively. Both take in three inputs.
function x = test1(a,b,c)
function [x,y] = test2(a,b,c)
If your function declaration looks like this
function test1(a,b,c)
and you try to get outputs from it with
x = test1(in1, in2, in3)
in your other code, then you will get a "Too many output arguments" error.

More Answers (1)

Jaden
Jaden on 26 Jun 2012
Yeah, I don't think that I have the function declared right. However, the problem is that there are way too many "ouputs" in my program that I don't want used as outputs, but are simply used as parts of the transformations.
I'll paste some example code:
this is how I call the function:
function [dataValues] = transformingdata(fileName);
and this the function itself, where you can see that there are a lot of variables going on:
function transformingdata(laser)
%This function takes any polar laser data set and simply transforms it into Cartesian.
%% Determining height and width of transformed scan
[height, width] = size(laser);
%% Determine angles for xvalues
% Laser Radian Step Size == .00436
if(width == 720) xvalues = [95:-0.263541667:-94.75]; % FOR 190 DEGREES end
if(width == 1081) xvalues = [135:-0.2498169664173166:-135]; % FOR 270 DEGREES end
%% Transform degrees into radians
xvalues = ((xvalues*pi)/180);
%% Initialize values
numberOfScans = 1; dataPlot = []; plot2 = []; X = []; Y = []; plotY = []; plotX = [];
%% Transformation loop
while (numberOfScans < height)
[X, Y] = pol2cart(xvalues,laser(numberOfScans, :));
if (numberOfScans == 1)
Y = Y;
X = X;
end
if(numberOfScans > 1)
plotY = [plotY; Y];
plotX = [plotX; X];
end
plot2 = [];
plot2(:,:,1) = [plotY];
plot2(:,:,2) = [plotX];
numberOfScans = numberOfScans + 2;
end
%%New size
[he, wi] = size(plot2(:,:,1));
%%Initialize variables
numberOfScan = 1;
plot2(:,:,3) = zeros(he,wi);
%%Creating translation matricies
TX = [.09];
TY = [.25];
TZ = [-.22];
TX = TX*ones(1,wi);
TY = TY*ones(1,wi);
TZ = TZ*ones(1,wi);
%%Center of robot loop
while (numberOfScan < he)
dataPlot(numberOfScan,:,1) = plot2(numberOfScan,:,1) + TX;
dataPlot(numberOfScan,:,2) = plot2(numberOfScan,:,2) + TY;
dataPlot(numberOfScan,:,3) = plot2(numberOfScan,:,3) + TZ;
numberOfScan = numberOfScan + 1;
end
dataValues = dataPlot;
robot = 'done_data!'
end
  1 Comment
Walter Roberson
Walter Roberson on 26 Jun 2012
Any variable whose name does not appear on the left side of the "function" statement (and which is not a global or nested variable) will be deleted when the function exit, so it is not a problem to create as many variables as you need to calculate the values that you want to return.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!