To store plotregression value in a variabe

is it possible to store the regression (R) value in a variable
%%plots the linear regression of targets relative to outputs.
plotregression(targets,outputs)

 Accepted Answer

Suppose the example given below
it is matlab example
[x,t] = simplefit_dataset;
net = feedforwardnet(10);
net = train(net,x,t);
y = net(x);
v =figure,
plotregression(t,y,'Regression')
%% To get Data
Data_X = v.Children(3).Children(1).XData;
Data_Y = v.Children(3).Children(1).YData;
%% To get Fit
Fit_X = v.Children(3).Children(2).XData;
Fit_Y = v.Children(3).Children(2).YData;
%% To get YeqT

7 Comments

actually i want to store "R" value in some variable.
you mean the R value shown in title?
if yes, try this
str =v.Children(3).Title.String;
R =str2double(str(find(str=='=')+1:end));
2nd way is
[R,~,~] = regression(t,y);
veeresh g
veeresh g on 5 Apr 2020
Edited: veeresh g on 5 Apr 2020
can you explain briefly..how to use those commands with exAmple..useusally i use plotregression(t,y,'Regression') command..i want to store R value which is shown on figure top..plese find attached figure for more clarity. in that R=0.36494 i want to store in some varaible
The Long Method
When you use plotregression command, it plots 3 Things
  1. Data
  2. Fit
  3. Y = T
Along with that there are titles, xlabel, ylabel and legends
We have the figure handle (we saved it in a variable named v)
v = figure;
plotregression(t,y,'Regression')
the other way to get handle is
v = plotregression(t,y,'Regression');
plotregression commands gives you figure handle as an output too.
After plotregrssion plots the figure, we access its handle as all the data related to that figure is saved in that handle. Remeber once you close the figure you cannot access the data anymore.
Type the following in command window
v.Children
You will se this, it shows that your figure has 3 children
  1. UIControl
  2. Legend
  3. Axes
We are interested in Axes as regression value is stored in axes title
Type the following in command window
v.Children(3)
you will se that this is the axes handle and you have all the properties of axes like title, xlabel, ylabel etc. Also all the values of Data, Fit an Y=T are stored in axes childrens.
Type following in command window to see Title properties
v.Children(3).Title
you will see that it's a text type and all info is shown. What we want is the string value Which is
'Regression: R=0.2145'
acess this string via
v.Children(3).Title.String
your ans will be
'Regression: R=0.2145'
Store it in some variable as i did in str in the above answer
str = v.Children(3).Title.String;
Now what you want is the value written after equal to sign, so find the equal to sign in str using following command
ind=find(str=='=');
now it will tell you the location of equal to in str
go to next index and you R value starts
R_in_str =str(ind+1:end);
Now this will give you the value of R but in the form of string
'0.2145'
convert it into double
R=str2double(R_in_str)
Your R value is Ready
The Short Method
Remeber that you can access R value directly using regression command. Go to help and type regression, it has the following sytax
[r,m,b] = regression(t,y)
when r is the regresson value you are getting on your plot title
so simply type
[R,~,~] = regression(t,y);
and your R value is in your hand
thanq you so much sir..it help me a lot ...actually.. i need to store R value for 200 iterations for each iteration.. i want to check how much R value.. may it help me a lot..
Awesome. I was searching for this since July.
Thank you

Sign in to comment.

More Answers (0)

Products

Asked:

on 3 Apr 2020

Commented:

on 26 Sep 2021

Community Treasure Hunt

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

Start Hunting!