Turn code into function

1 view (last 30 days)
Paula Barreiro
Paula Barreiro on 14 Sep 2022
Edited: Jan on 14 Sep 2022
FP= {'Please enter three frequencies [f1 f2 f3]:'};
prompt_title = 'Frequency Data?';
dims = [1 1000];
default = {'1 1 1'};
FP_index = inputdlg(FP, prompt_title, dims, default);
FP_MATRIX = cell2mat(FP_index)
AMP= {'Please enter three amplitudes [a1 a2 a3]:'};
prompt_title1 = 'Amplitude Data?';
dims = [1 1000];
default = {'1 1 1'};
AMP_index = inputdlg(AMP, prompt_title1, dims, default);
AMP_MATRIX = cell2mat(AMP_index)
T= linspace(1,3,1000)
Sin1= AMP_MATRIX(1)*sin(2*pi*FP_MATRIX(1)*T)
Sin2= AMP_MATRIX(2)*sin(2*pi*FP_MATRIX(2)*T)
Sin3 = AMP_MATRIX(3)*sin(2*pi*FP_MATRIX(3)*T)
hold on;
Sin= Sin1 + Sin2 +Sin3
legend('S_1','S_2','S_3','Output signal');
title('Plots of three signals and sum')
hold off
My code is above I am trying to turn this whole thing into a function using one line of code.

Answers (1)

Jan on 14 Sep 2022
Edited: Jan on 14 Sep 2022
Instert one line on top:
function ANameOfYourchoice
and save the file as "ANameOfYourchoice.m".


Find more on Entering Commands in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by