Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding specific letters to a string MATLAB

I'm a neuroscience/biomedical engineering major struggling with this whole MATLAB programming ordeal and so far, this website is the best teacher available to me right now. I am currently having trouble with one of my HW problems. What I need to do is take a phrase, find a specific word in it, then take a specific letter in it and increase that letter by the number indicated. In other words:

phrase = 'this homework is so hard'
word = 'so'
letter = 'o'
factor = 5
which should give me 'This homework is sooooo hard'

I got rid of my main error, though I really don;t know how. I exited MATLAB, then got back into it. Lo and behold, it magically worked.

function[out1] = textStretch(phrase, word, letter, stretch)
searchword= strfind(phrase, word);
searchletter strfind(hotdog, letter); %Looks for the letter in the word
add = (letter+stretch) %I was hoping this would take the letter and add to it, but that's not what it does 
replace= strrep(phrase, word, add) %This would theoretically take the phrase, find the word and put in the new letter
out1 = replace

According to the teacher, the ones() function might be useful, and I have to concatenate strings, but if I can just find it in the string and replace it, why do I need to concatenate?

like image 220
Jessica Marie Avatar asked Dec 08 '25 10:12

Jessica Marie


2 Answers

Since this is homework I won't write the whole thing out for you but you were on the right track with strfind.

a = strfind(phrase, word); 
b = strfind(word, letter); 

What does phrase(1:a) return? What does phrase(a+b:end) return?

Making some assumptions about why your teacher wants you to use ones:

What does num = double('o') return? What does char(num) return? How about char([num num])?

You can concatenate strings like this:

out = [phrase(1:a),'ooooo',phrase(a+b:end)];

So all you really need to focus on is how to get a string which is letter repeated factor times.

If you wanted to use strrep instead you would need to give it the full word you are searching for and a copy of that word with the repeated letters in:

 new_phrase = strrep(phrase, 'so', 'sooooo');

Again, the issue is how to get the 'sooooo' string.

like image 60
nkjt Avatar answered Dec 10 '25 10:12

nkjt


See if this works for you -

phrase_split = regexp(phrase,'\s','Split'); %// Split into words as cells
wordr = cellstr(strrep(word,letter,letter(:,ones(1,factor))));%// Stretched word
phrase_split(strcmp(phrase_split,word)) = wordr;%//Put stretched word into place
out = strjoin(phrase_split) %// %// Output as the string cells joined together

Note: strjoin needs a recent MATLAB version, which if unavailable could be obtained from here.

Or you can just use a hack obtained from the m-file itself -

out = [repmat(sprintf(['%s', ' '], phrase_split{1:end-1}), ...
             1, ~isscalar(phrase_split)), sprintf('%s', phrase_split{end})]

Sample run -

phrase =
this homework is so hard and so boring
word =
so
letter =
o
factor =
     5
out =
this homework is sooooo hard and sooooo boring

So, just wrap the code into a function wrapper like this -

function out = textStretch(phrase, word, letter, factor)

Homework molded edit:

phrase = 'this homework is seriously hard'
word = 'seriously'
letter = 'r'
stretch = 6

out = phrase
stretched_word = letter(:,ones(1,stretch))

hotdog = strfind(phrase, word)
hotdog_st = strfind(word,letter)
start_ind = hotdog+hotdog_st-1
out(start_ind+stretch:end+stretch-1) = out(start_ind+1:end)
out(hotdog+hotdog_st-1:hotdog+hotdog_st-1+stretch-1) = stretched_word

Output -

out =
this homework is serrrrrriously hard

As again, use this syntax to convert to function -

function out = textStretch(phrase, word, letter, stretch)
like image 43
Divakar Avatar answered Dec 10 '25 08:12

Divakar