将特定字母添加到字符串MATLAB

时间:2014-09-19 14:01:26

标签: matlab str-replace findstr strcat

我是一名神经科学/生物医学工程专业,在整个MATLAB编程考验中苦苦挣扎,到目前为止,这个网站是我现在可以使用的最好的老师。我目前遇到了一个我的硬件问题。我需要做的是拿一个短语,在其中找到一个特定的单词,然后在其中带一个特定的字母,并按照指示的数字增加该字母。换句话说:

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

我摆脱了我的主要错误,虽然我真的不知道如何。我退出MATLAB,然后回到它。瞧,它神奇地工作了。

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

根据老师的说法,ones()函数可能很有用,我必须连接字符串,但如果我能在字符串中找到并替换它,为什么我需要连接?

3 个答案:

答案 0 :(得分:2)

由于这是作业,我不会为你写出整件事情,但是你在strfind的正确轨道上。

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

phrase(1:a)返回什么? phrase(a+b:end)返回什么?

对您的老师希望您使用ones的原因做出一些假设:

num = double('o')返回什么? char(num)返回什么? char([num num])怎么样?

您可以连接这样的字符串:

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

所以你真正需要关注的是如何获得letter重复factor次的字符串。

如果您想使用strrep,则需要提供您要搜索的完整字词以及该字词的副本,并带有重复的字母:

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

同样,问题是如何获得' sooooo'字符串。

答案 1 :(得分:1)

看看这是否适合你 -

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

注意:strjoin需要最近的MATLAB版本,如果不可用,可以从here获得。

或者您可以使用从m文件本身获取的黑客 -

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

示例运行 -

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

所以,只需将代码包装成像这样的函数包装器 -

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

家庭作业模塑编辑:

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

输出 -

out =
this homework is serrrrrriously hard

再次使用此语法转换为函数 -

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

答案 2 :(得分:0)

Jessica首先这是错误的,但我不是在这里给你解决方案。你能用这种方式吗?这肯定会运行。

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