# Output Local Variables from a Function - R2, F-Test, Stats, etc.

6 ビュー (過去 30 日間)
10B 2015 年 9 月 22 日
コメント済み: 10B 2015 年 9 月 22 日
Hello Community,
I have written a function to try to output some key observations about some base data (X,Y). I calculate the r, r2, f-test etc. from the data including calling in the built-in function 'stats'. Code as:
function [r,r2 h,p,ci,stats] = R2Ftest( X, Y )
c=corrcoef([X Y]);
r=c(1,2);
r2=r^2;
figure;
plot(X,Y,'+r');
rsquared = num2str(r2);
title(['XXXX'])
xlabel('XXXX')
ylabel('XXXX')
h = vartest2(X,Y)
[h,p,ci,stats] = vartest2(X,Y,'Alpha',0.01)
Now, everything outputs nicely in my command window, but really I want to have this information output as variables in my workspace that I can subsequently save.
I know I want to avoid using 'global' (I've read enough about this), and thought I had assigned the output arguments as I wanted - but no joy. So, any thoughts on the fix for this?
10B.

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

### 採用された回答

Stephen 2015 年 9 月 22 日

Instead of calling the function like this:
R2Ftest(X,Y)
simply call the function with those outputs:
[r,r2 h,p,ci,stats] = R2Ftest(X,Y)
and they will appear in that workspace. You have defined those outputs inside the function, but it only when it is called do they appear in the calling workspace.
You might like to review the MATLAB tutorials, which cover basic usage like this:
##### 1 件のコメント表示非表示 なし
10B 2015 年 9 月 22 日
Ah - Thanks Stephen. Another schoolboy error on my behalf, all the more amusing as I had actually written the answer myself at the start of the function! I will now know for next time.
10B

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

### Community Treasure Hunt

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

Start Hunting!

Translated by