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

1 view (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 22 Sep 2015
Edited: Stephen23 on 22 Sep 2015
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 Comment
10B
10B on 22 Sep 2015
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

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!