passing arguments between function files

Anyone know how to allow multiple files as input for one function file?

4 件のコメント

Jan
Jan 2012 年 6 月 30 日
Please explain any details of your problem.
anna
anna 2012 年 7 月 2 日
not going to explain this very well but;I have 2 function files, call them funcX, funcY which have as output 2 parameters, X and Y and I want them to be read by a third function file funcZ as input to form say, Z from those X and Y. What syntax should I use to pass them in those 3 files?
Jan
Jan 2012 年 7 月 3 日
Do you mean something like: Z = max(sin(0.2), cos(0.3))? max, sin and cos are functions also.
Usually it helps to understand the problem, if you post, what you have tried so far.
Walter Roberson
Walter Roberson 2012 年 8 月 24 日
(No obvious reason to close this question, but one of the Answers should be Accepted.)

回答 (2 件)

Image Analyst
Image Analyst 2012 年 6 月 30 日
編集済み: Image Analyst 2012 年 7 月 3 日

0 投票

% Define a function to take mulitple filenames and return multiple outputs.
function [output1 output2 output3] = yourFunction(filename1, filename2, filename3)

4 件のコメント

anna
anna 2012 年 7 月 2 日
thanks! that seemed to help somehow but not completely...
Image Analyst
Image Analyst 2012 年 7 月 3 日
Why not? That's how you define a function to take multiple inputs and return multiple outputs. You call it almost the same way, just don't use the function word:
% Call the function.
[output1 output2 output3] = yourFunction(filename1, filename2, filename3);
anna
anna 2012 年 7 月 4 日
thank you! and sorry, no, you're right, I know that much. i just didnt explain my question well. what I meant was that i have 2 separate function files that have as output X and Y and I want them to both be passed into a new function file, funcZ. how should i both call and define the function?
Image Analyst
Image Analyst 2012 年 7 月 15 日
If funcx and funcy both return an x and a y, how about
[x1 y1] = funcx();
[x2 y2] = funcy();
zOutput = funcz(x1, y1, x2, y2);
or using an alternate interpretation of your wording:
x = funcx();
y = funcy();
zOutput = funcz(x, y);
(This is what Walter said except that I'm capturing the outputs into arrays in the main program first before I pass them into funcz.)
Walter Roberson
Walter Roberson 2012 年 7 月 3 日

0 投票

Z = funcZ( funcX(), funcY() );
disp(Z);
function Z = funcZ( X, Y )
Z = .... appropriate code ...
end
function X = funcX()
X = .... appropriate code ...
end
function Y = funcY()
Y = .... appropriate code ...
end

2 件のコメント

anna
anna 2012 年 7 月 4 日
Thanks!this seems the closest to what I wanted...but in which file should I put each line of code? its giving me an error for Z = funcZ( funcX(), funcY() ); which im putting in files funcX and funcY. should it not be Z = funcZ( funcX(X), funcY(Y) )?
this: function Z = funcZ( X, Y ) Z = .... appropriate code ... end i have in funcZ.
Walter Roberson
Walter Roberson 2012 年 7 月 15 日
You said that X is an output of funcX and Y is an output of funcY, so you should not be passing X or Y into funcX and funcY
Put the following into testfuncZ.m :
Z = funcZ( funcX(), funcY() );
disp(Z);
Then put the funcZ code into funcZ.m and the funcX code into funcX.m and the funcY code into funcY.m . Invoke it all by giving the command
testfuncZ

この質問は閉じられています。

質問済み:

2012 年 6 月 30 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by