help with creating certain function for setting elements in one matrix to another

want to make function that takes in text, and then creates a matrix that sets each element to one of the ascii code elements
for example: [abcdef] would get set to [1 2 3 4 5 6]
and [help] would get set to [8 5 12 16]
pls help

Answers (1)

Here's one way:
alphabet = 'abcdefghijklmnopqrstuvwxyz';
[~,vec] = ismember('help',alphabet)

9 Comments

what if I want help to represent a vector... so like help could represent a certain a bunch of different letters help = jsdbsfb
how would I get it to convert 'jsdbsfb' and not 'help'
[~,vec] = ismember('jsdbsfb',alphabet)
or
charvec = 'jsdbsfb';
[~,vec] = ismember(charvec,alphabet)
Just define it as a variable, and pass that variable.
str = 'jsdbsfb';
[~,vec] = ismember(str,alphabet)
Note that a simpler way to generate the alphabet is to use the colon, which also reduces the chance of missing a letter:
str = 'jsdbsfb';
[~,vec] = ismember(str,'a':'z')
okay yes this would work, but in my project 'jsdbsfb' is actually a random matrix of 300+ letters, so I can't type them into Matlab. I stored them all as one vector called 'text1'
So, maybe next time give a complete description of your problem first?
"I stored them all as one vector called 'text1'"
Stored where? In some file, in a matrix in MATLAB, or printed on a piece of paper? How did you define this matrix?

This question is closed.

Asked:

on 4 Nov 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!