×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Matlab random string

Matlab random string

Matlab random string

(OP)
Hi there

I would like to ask if there is any random string generator function in Matlab.

Thanks for your time

RE: Matlab random string

Hey,

I don't believe there is one but it should be a fairly simple function to code. I can make one for you if you want?

David

RE: Matlab random string

(OP)
Dear David

Many thanks for your interest. I would be of course really obliged if you could do that.

Manos

RE: Matlab random string

Hey Attached is my RandomString(length) function. As you can see it requires a length as input and then outputs the required result.

If you want to add more letters of characters to the list of what can be generated you are free to (as long as the number of characters doesn't exceed 100 the script will still work).

David

RE: Matlab random string

Now that is what I call bad Matlab programming!

Try these functions:
The first one generates a random string where repetitions are allowed. The second one generates a random string where repetitions are not allowed.

CODE

function String = RandomString1(n)

% generates a random string of lower case letters of length n

LetterStore = char(97:122); % string containing all allowable letters (in this case lower case only)
String = LetterStore(ceil(length(LetterStore).*rand(1,n)));

CODE

function String = RandomString2(n)

% generates a random string of lower case letters of length n
% n must be less than the number of possible letters
% elements of the returned string are exclusive

LetterStore = char(97:122); % string containing all allowable letters (in this case lower case only)
Idx = randperm(length(LetterStore));
String = LetterStore(Idx(1:n));

M


--
Dr Michael F Platten

RE: Matlab random string

Thanks MikeyP, :S

All I can say is my function works and I don't know all the builtin functions because I don't use it all that often.

RE: Matlab random string

All I can say then IrishDave is you have only scratched the surface of a really neat bit of software.  Many people across the World waste hours trying to out-do each other in squeezing a solution into as few lines as possible (Matlab "golf").  Had this thread been in the Matlab newsgroup your solution would have been mercilessly mocked and MikeyP's would have appeared almost immediately in all sorts of similar forms by the regular golfers there.

Want to learn all the tricks?  Frequent that group as well as this site.  The group's FAQ is good too.

RE: Matlab random string

I do like Matlab because it allows me to test things in a simple programming language but my main domain is fluid simulations so using Matlab just doesn't work because of the size of my fields, it quickly just isn't quick enough when compared with C++.

I can understand that my solution wasn't the most Matlabish in style, but it still worked and it's not bad programming, just not using the Matlab functions. :)

David

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources