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

1 次查看(过去 30 天)
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?
Thanks in advance.
10B.

采纳的回答

Stephen23
Stephen23 2015-9-22
编辑:Stephen23 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
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by