change a script to function
5 views (last 30 days)
Show older comments
Hi all,
this question is probably trivial to some of you but I'm wondering how to change my script to a function. So far, I've been using it just for myself and had no need for it. However, I've been asked to make it user friendly so colleague's can use it and decided it was a good moment to also learn to work with GUIDE. I've worked out the first part, loading information from excel. Second part will be to manipulate that data. One script I was using is the following and before going further with GUIDE I'd like to figure out how to make this into a function.
if true
Spectra = cell(nbins,2); %Generates an empty Cell for storing the different bin sizes and counts.
for x = 1:nbins %number of different bin sizes to be evaluated
for y = 1:round((length(PGE(:,1))-500)/x) %PGE is the .mat file containing the excel data
Spectra{x,1} = PGE(1:y,1).*x; %Creates x-axis for each each bin size
end
end
for x = 1:nbins
for y = 1:length(Spectra{x,1})
Spectra{x,2}(y,1) = sum(PGE(1+((y-1).*x):x*y,2)); %store Counts
end
end
save Spectra.mat
end
Thanks in advance :)
Rick
0 Comments
Accepted Answer
Aakash Deep
on 15 May 2018
Hey,
In order to create a function in MATLAB, add a couple of lines to your code
function [out_1, out_2, ....., out_n] = function_name(in_1, in_2, ....., in_n)
….
<your script>
….
end
where,
out_k is your output variables from the function ( k = 1:n )
in_k is your input variables which you will pass into your function ( k = 1:n ), if you don't have any input parameters leave the parenthesis blank.
Note: function_name and filename should always be same, otherwise your code will throw an error.
For more details on functions in MATLAB and examples, please refer the following link, https://www.mathworks.com/help/matlab/ref/function.html
Hope this helps
Thanks,
Aakash Deep
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import from MATLAB 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!