MATLAB Answers

How would I convert a script to a function?

19 views (last 30 days)
I have just made a script randArrGen.m that generates an array of n floating point numbers, which needs to be used as a function within another scipt, but I'm struggling to understand how to turn it into a function. In my head it seems easier just to run this script within the other script when required. The a and b values aren't massively important and are just to give a range for the floating point numbers to be within (I though -1000 and 1000 were good boundaries).
% randArrGen generates an array of n floating point numbers, where n is a
% value inputted by the user.
n = input("How many values do you wish to have in the array?");
a = -1000;
b = 1000;
array = (b-a).*rand(n,1) + a;

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Jan 2021
Put a function line on it:
function array = randArrGen()
% randArrGen generates an column vector of n floating point numbers,
% where n is a value inputted by the user.
n = input("How many values do you wish to have in the array?");
a = -1000;
b = 1000;
array = (b -a ) .* rand(n, 1) + a;

  6 Comments

Show 3 older comments
Image Analyst
Image Analyst on 7 Jan 2021
Most people would probably ask the user for parameters before calling the function. You could use inputdlg() to ask for n, a, and b with defaults. Then to call it:
Main script:
array = randArrGen(n, a, b);
File : randArrGen.m
function array = randArrGen()
% randArrGen generates an column vector of n floating point numbers, in the range [a, b],
% where n, a, and b are values passed in through the input argument list.
array = (b -a ) .* rand(n, 1) + a;
Louise Wade
Louise Wade on 7 Jan 2021
The main script where I have to use the function has a line to ask for the parameter for n, and I'm keeping a and b set at specific values just to give a nice range that isn't too massive. Thank you for the help with this. It's starting to make a lot more sense to me.
Image Analyst
Image Analyst on 7 Jan 2021
Try this:
% Ask user for three floating point numbers.
defaultValue = {'20', '-1000', '1000'};
titleBar = 'Enter values';
userPrompt = {'Enter n : ', 'Enter a: ', 'Enter b: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
n = str2double(caUserInput{1})
a = str2double(caUserInput{2})
b = str2double(caUserInput{3})
% Check usersValue1 for validity.
if isnan(n)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into n.
n = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', n);
uiwait(warndlg(message));
end
% Do the same for a
% Check a for validity.
if isnan(a)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into a.
a = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', a);
uiwait(warndlg(message));
end
% Do the same for b
% Check b for validity.
if isnan(b)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into b.
b = str2double(defaultValue{3});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', a);
uiwait(warndlg(message));
end
Good luck, Hopefully you've been through this:

Sign in to comment.

More Answers (1)

Jan
Jan on 7 Jan 2021
All you have to do to convert it to a function, is to add a head line:
function array = randArrGen
Maybe you want to define n and/or a, b as input arguments?

  1 Comment

Louise Wade
Louise Wade on 7 Jan 2021
Thanks. I didn't realise it would be that simple. I'm super new to coding stuff, so I'm wrapping my head around the basics currently.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by