How to make this code as a function?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
on 25 Mar 2015
0 votes
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
Star Strider
on 26 Mar 2015
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?
R7 DR
on 26 Mar 2015
Star Strider
on 26 Mar 2015
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.
R7 DR
on 27 Mar 2015
Star Strider
on 27 Mar 2015
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.
R7 DR
on 27 Mar 2015
Star Strider
on 27 Mar 2015
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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!