How to return multiple variables in a function call in MATLAB R 2015b?

6 ビュー (過去 30 日間)
Kallam Haranadha Reddy
Kallam Haranadha Reddy 2018 年 11 月 6 日
回答済み: Steven Lord 2018 年 11 月 6 日
I have written a function as written below. function [avgfit,pipop,bestfit]= gafitness(psz,n,pop)
But when i called this function in another script m-file , with the following line [avgfit,pipop,bestfit]= gafitness(psz,n,pop); it is returning only the fist output variable(avgfit).other output variables (pipop,besrfit) are shown as undefined.
I want to know how to modify the function call to return multiple output variables.
  3 件のコメント
Jan
Jan 2018 年 11 月 6 日
What does "shown as undefined" mean? Please post the complete error message, if there is one.
Kallam Haranadha Reddy
Kallam Haranadha Reddy 2018 年 11 月 6 日
This is the function definition.
function [ avgfit,fitpop,pipop,bestfit] = InvClassifyGAFitnessFunc( classGA,ClassDM )
-------
------
code
-----
-----
fitpop = (1/t)*sum(pipop(i,1));
avgfit=fitpop;
%fitpop=1/fitpop;
bestfit=max(pipop);
end
This is the function call in the script file
ABC_GA
[pipop,fitpop, avgfit,bestfitt ] = InvClassifyGAFitnessFunc( classGA,ClassDM );
when i ran this ,it is displaying the error message Undefined function or variable 'bestfit'.
Error in ABC_GA (line 30) bestfit;

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2018 年 11 月 6 日
First, the order of the output arguments in your function definition and in your function call are different. That's a flag to me that you may not be storing the data you expect into the variable you expect. Just because the names you use in your function call match the names in the function definition does not mean that MATLAB will automatically return the data in the order in your call. The contents of the variable avgfit (the first output in your function declaration) from inside the function will be stored in the variable pipop (the first output in your function call) in the calling function not the third (because the name avgfit is in the third output position in your function call.)
Second, in your function call the fourth output argument is named bestfitt, with two t's at the end. The error message says that the variable bestfit with one t at the end is undefined. Fixing that typo will eliminate the error message on that line, but because of the first issue I mentioned it may not make your function work as expected.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by