3

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?

Jessica Marie
  • 293
  • 5
  • 16
  • do you have the function on MATLAB path? or built-in the main function? If not that would be the reason why you have "Undefined function 'textStretch' for (...)" – ASantosRibeiro Sep 19 '14 at 14:09
  • 1
    Your code clearly won't work - you can't give `strrep` a letter and a number and expect it to know to repeat the letter five times - it's "string replace" not "string repeat". You're also calling `strfind` twice and then doing nothing with the outputs. My suggestion is to start by working out how to start with a word ('so'), letter ('o') and number (5) and get 'sooooo' out. Once you've done that, you can use `strrep`. – nkjt Sep 19 '14 at 14:12
  • It's in the same place as all my other codes, so I don't know why it's giving me an issue now. – Jessica Marie Sep 19 '14 at 14:13
  • as refered it will not work in any case. but that should not be the reason for the error presented. make sure you are on the path where this function is defined, or you added this function path (for example addpath(path to function) – ASantosRibeiro Sep 19 '14 at 14:15
  • 1
    @JessicaMarie I strongly suggest you reconsider your variable names to something more meaningful! – Dan Sep 19 '14 at 14:30
  • @ASantosRibeiro It's on the pathway, it just doesn't seem to like the whole 'char' aspect And my variable names are for my own amusement. I comment and explain everything so the names are irrelevant – Jessica Marie Sep 19 '14 at 14:57
  • @JessicaMarie I posted one answer with how you must use such function. Note however that this does not solve your Homework, just the problem you have currently – ASantosRibeiro Sep 19 '14 at 15:07
  • @JessicaMarie - You've got a kinky sense of humour with your variable names. – rayryeng Sep 19 '14 at 15:08
  • @rayryeng ...you know, I didn't even realize that. I was using hotdog from an earlier code where I wanted more than one vowel in it to remove. This whole problem is supposed to be about showing flirtiniss or excitement using matlab lol – Jessica Marie Sep 19 '14 at 15:10
  • @JessicaMarie - hahah that's ok... it was hard to keep my composure seeing how flirty hotdogs and colddogs could be when they hug each other. It's almost as if it you're telling a story.... on a serious note, maybe rename the variables so that they're showing us what you're trying to do. I don't think that story is representative of adding letters to a string ;) – rayryeng Sep 19 '14 at 15:13
  • @rayryeng I'm really an English major. I renamed them and added in comments on what I was attempting to do with my code. – Jessica Marie Sep 19 '14 at 15:19
  • @JessicaMarie - Being an English major is totally fine. Thanks for the edits :) – rayryeng Sep 19 '14 at 15:20

3 Answers3

2

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.

nkjt
  • 7,825
  • 9
  • 22
  • 28
  • phrase(1:a) should give me the word I want. phrase(a+b:end) should give me My word and my letter. num = double('o') should give me 'o'? char(num) makes it into a character. char([num num]) makes it into an array? The TAs hinted that strrep would be the right way to go about this. I am still unsure about where ones() comes in. Unless that has to deal with my factor. – Jessica Marie Sep 19 '14 at 15:03
  • No on the indexing into `phrase`. Read the documentation on `strfind` again, or try it out on the commandline. No on `double` - again, read the docs on `double`/`char`. `ones` comes in when you're trying to construct a char array of an arbitrary size (based on "factor"). – nkjt Sep 19 '14 at 15:09
  • strfind gives me the vectors/indices of what I'm searching. Double and char are like different triggers for MATLAB. Character is for like strings I thought. So I would use ones with factor. Alright – Jessica Marie Sep 19 '14 at 15:28
1

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)
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • I think the general idea was so that she could do her homework, but well this is the way to do it – ASantosRibeiro Sep 19 '14 at 15:01
  • @ASantosRibeiro It's hard to draw a line between what's help and what's the solution. But I get your point, just didn't know where to stop :) Also, at least OP is honest about it and has shown the work! – Divakar Sep 19 '14 at 15:03
  • I still have to figure out why I can't run my code so I can't tell if that works or not, but I would rather use techniques I actually have some experience with. I haven't learned about cellstr, or phrase_split and while I technically know regex, I don't know it well enough. So while I'm sure this code is correct, I am not entirely comfortable using it, but here's a heart in thanks anyway <3 It's appreciated – Jessica Marie Sep 19 '14 at 15:06
  • @JessicaMarie Check out the hearty edits! No cells or cell arrays, but keeping the `ones` as you said might be what they are looking for. Also, keep in mind this is for replacement of first of such words only, but you can modify this to make it work for multiple matches of course. – Divakar Sep 19 '14 at 15:38
  • That works actually :) Now I'm not entirely going to use it(not comfortable doing that at all), but it gives me something to go off of. You got very index happy, but I am pretty sure I can follow it. I agree with the output statement. It's hard when the most your teacher did with strings is type them up, vaguely show you what they did, then moved on or talked about the upcoming football game. This is a teach yourself class – Jessica Marie Sep 19 '14 at 15:52
  • @JessicaMarie You said you were excited/happy, so got those "nice" variables names? Well if I am told what to use and what not to (specially about this what not to part), I would rather have a different feeling about it and might use some horrible variables names just to piss them off! :) – Divakar Sep 19 '14 at 16:00
  • I can take recommendations, but I mean they are my variable names. For the sake of people understanding what I'm doing, I can see the importance of being a bit more clear with naming, but my first issue was the whole function not liking me thing – Jessica Marie Sep 19 '14 at 16:04
0

Well Jessica first of all this is WRONG, but I am not here to give you the solution. Could you please just use it this way? This surely run.

function main_script()
phrase = 'this homework is so hard';
word = 'so';
letter = 'o';
factor = 5;
[flirty] = textStretchNEW(phrase, word, letter, factor)
end

function [flirty] = textStretchNEW(phrase, word, letter, stretch)
hotdog = strfind(phrase, word);
colddog = strfind(hotdog, letter);
add = letter + stretch;
hug = strrep(phrase, word, add);
flirty = hug
end
ASantosRibeiro
  • 1,247
  • 8
  • 15