change a script to function

조회 수: 59(최근 30일)
Rick Verberne
Rick Verberne 2018년 5월 15일
답변: Aakash Deep 2018년 5월 15일
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
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
save Spectra.mat
Thanks in advance :)

채택된 답변

Aakash Deep
Aakash Deep 2018년 5월 15일
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>
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,
Hope this helps
Aakash Deep

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by