## How to make this code as a function?

### R7 DR (view profile)

on 25 Mar 2015
Latest activity Commented on by Star Strider

### Star Strider (view profile)

on 27 Mar 2015
Hi
I want to make the following code as a function. I want to call this code as function in another code to get the 'T' values.
for i=1:8
x(1)=0.1
x(2)=0.2
x(i+1)=x(i)+0.1
for j=2:2242
if x(i)>alpha_20(j)
X2_20(i)=alpha_20(j+1)
X1_20(i)=alpha_20(j)
Y2_20(i)=T_20(j+1)
Y1_20(i)=T_20(j)
end
end
T (i)=(((Y2_20(i)-Y1_20(i))/(X2_20(i)-X1_20(i)))*(x(i)-X1_20(i)))+Y1_20(i)
end
Thanks

### Tags ### Star Strider (view profile)

on 25 Mar 2015

See the documentation for Function Basics to get you started.
You already know you want it to return ‘T’, so you need to define what (if any) inputs you want for your function

Star Strider

### Star Strider (view profile)

on 27 Mar 2015
My pleasure!
To return both ‘T’ and ‘D’, the first line of your function becomes:
and your call to it becomes:
That should work.
Those are the only changes needed.
R7 DR

### R7 DR (view profile)

on 27 Mar 2015
Thanks. Its working. I did a mistake by calling the 'T' and 'D' separately in the main code.
I came up with one more doubt. I need to plot the graph between the 'T_a' (loaded from excel) and 'D'(calculated in the loop) for each file in a single figure. As we are not storing the data loaded from excel files, how to make the graph?
we must store the 'T_a' values to plot the graph (or) we have some other alternative approach?
Thanks
Star Strider

### Star Strider (view profile)

on 27 Mar 2015
You can always return the ‘T_a’ values just by adding it to the returned values: