change a script to function

5 次查看(过去 30 天)
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

采纳的回答

Aakash Deep
Aakash Deep 2018-5-15
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 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by