How to make this code as a function?

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.
[T_20]=xlsread('20.xlsx','sheet2','C2:C2243')
[alpha_20]=xlsread('20.xlsx','sheet2','F2:F12243')
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

Answers (1)

Star Strider
Star Strider 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

8 Comments

R7 DR
R7 DR on 26 Mar 2015
Edited: R7 DR on 26 Mar 2015
Thanks for the reply.
My aim is to calculte the 'T' value from 20 excel files by using the above code. The calculation loop is same for all the excel sheets, only change is with excel files.
If I write the code in the same file, it is too lengthy. I have 2 ideas, first one is to create an indivitual code or file for each excel sheet and at the end load all the 'T' values in to one variable with one final matlab code.
Now, the question is that how to call the data from those individual files??
2) I thought it can be done by creating a function. I just load the excel data and run the calculation loop as a function.
I dont know whether they are good approaches or not, If you know alternative method please let me know.
Thanks for your time.
My pleasure.
I’m not certain that I understand exactly what you want to do. If the Excel sheets are all the same format so the column references don’t change, but only the file name changes, then if I understand correctly, you simply need to pass the file prefix (as a string) to your function and have it return ‘T’.
For instance, your function (I named it ‘ExcelRead’ here) would have its first three lines as:
function T = ExcelRead(prefix)
[T_20]=xlsread([prefix '.xlsx'],'sheet2','C2:C2243')
[alpha_20]=xlsread([prefix '.xlsx'],'sheet2','F2:F12243')
... REST OF CODE ...
end
and you would then call it as:
prefix = '20';
T = ExcelRead(prefix);
If you’re going to call it several times in a single function and need to store all the ‘T’ values, consider saving ‘T’ as a multi-dimensional array or a cell array, adding a new ‘page’ or cell element for each call.
For example:
for k1 = 1:something
prefix = filenumberstring(k1,:);
T(:,:,k1) = ExcelRead(prefix);
... OTHER CODE ...
end
or for a cell array, the ‘ExcelRead’ call would be:
T{k1} = ExcelRead(prefix);
Note the curly brackets ‘{}’ indicating a cell element.
Is this the sort of thing you want to do?
Thanks for the help.
The first part of the code is working perfect. This is amazing,now my code is reduced to few lines :) :)
when I tried to store the 'T' values. It is shwoing the following error.
Undefined variable filenumberstring.
Error in test (line 5)
prefix = filenumberstring(k1,:);
In this line of code ''for k1 = 1:something'' is something is the maximum file number?
Is is possible to assign the calculated 'T' values to a new variable based on the file name?. For example, if the function calculates the 'T' value for the file name ''20.xlsx'' then the calculated T value should be assigned to a new variable 'T20'(here 20 represents the file name). Hope I explained clearly.
Thanks for your time.
The ‘filenumberstring’ variable is intended to represent whatever the prefix of your file is, depending on how you store it. It is yours to define. In your illustration, it might be:
filenumerstring = '20';
it could also be:
filenumber = [20, 25, 31, 42];
for k1 = 1:length(filenumber)
prefix = num2str(filenumber(k1), '%02d');
T{k1} = ExcelRead(prefix);
... ETC ...
end
or whatever you want.
the ‘something’ reference is the length of the array in which you have stored your file numbers.
Dynamically naming variables such as ‘T20’ for filename '20.xlsx' is not good programming practice. The ‘k1’ reference for each value of ‘T’ will keep them easily accessible. Each ‘T{k1}’ (using cell notation) corresponds to ‘filenumber(k1)’, where filenumber are the number names you hve given your files. It is then easy to cross-reference them by ‘filenumber’.
There are many different ways to structure your code, some better than others, and each way taken in the context of what you are doing. I haven’t seen how you’ve structured the rest of your code, so I’m guessing as to how to structure the loop.
Perfet!! The code is working super fine. Many thanks!!
Now I have one final question. In the function, is it possible to add one more term 'D' in addition to 'T'. In that case my for loop changes as below code..
[T_a]=xlsread([prefix '.xlsx'],'sheet2','C2:C2243')
[alpha_a]=xlsread([prefix '.xlsx'],'sheet2','F2:F2243')
[D_a]=xlsread([prefix '.xlsx'],'sheet2','G2:G2243')
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_a(j)
X2_a(i)=alpha_a(j+1)
X1_a(i)=alpha_a(j)
Y2_a(i)=T_a(j+1)
Y1_a(i)=T_a(j)
Z2_a(i)=D_a(j+1)
Z1_a(i)=D_a(j)
end
end
T (i)=(((Y2_a(i)-Y1_a(i))/(X2_a(i)-X1_a(i)))*(x(i)-X1_a(i)))+Y1_a(i)
D(i)=(((Z2_a(i)-Z1_a(i))/(X2_a(i)-X1_a(i)))*(x(i)-X1_a(i)))+Z1_a(i)
end
I tried to include 'D' in the code as below but it is giving the same values of 'T'.
D{k1} = ExcelRead(prefix);
Thanks for your time.
My pleasure!
To return both ‘T’ and ‘D’, the first line of your function becomes:
function [T,D] = ExcelRead(prefix)
and your call to it becomes:
[T{k1},D{k1}] = ExcelRead(prefix);
That should work.
Those are the only changes needed.
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
You can always return the ‘T_a’ values just by adding it to the returned values:
function [T,D,T_a] = ExcelRead(prefix)
and as before:
[T{k1},D{k1},T_a{k1}] = ExcelRead(prefix);

This question is closed.

Tags

Asked:

on 25 Mar 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!