Another way to create a string of lowercase letters?

1 view (last 30 days)
I'm trying to create a function that will create a string of as many lowercase characters from a-z as specified by the input.
Here's my code, which does work:
function [y] = func1(x)
s = 'abcdefghijklmnopqrstuvwxyz';
n = length(s);
slength = x;
y = s(round(rand(1,slength)*n));
end
How can I write this function instead using the randi and char built-in functions?

Answers (1)

Image Analyst
Image Analyst on 30 Oct 2014
Edited: Image Analyst on 30 Oct 2014
Try this:
stringLength = 40;
str = char(randi([97, 122], 1, stringLength))
  1 Comment
Image Analyst
Image Analyst on 13 Nov 2014
Just to save after you delete your question, you asked this:
I'm trying to create a function that will create a string of as many lowercase characters from a-z as specified by the input.
Here's my code, which does work:
function [y] = func1(x)
s = 'abcdefghijklmnopqrstuvwxyz';
n = length(s);
slength = x;
y = s(round(rand(1,slength)*n));
end
How can I write this function instead using the randi and char built-in functions?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!