循环生成的格式字符串

时间:2014-07-18 08:32:33

标签: string matlab loops text

我想要做的是从编辑a获取字符串,然后通过for循环修改它。我设法做到了这一点,但我在文本b中显示答案时遇到了问题。我设法找到了fprintf命令,但我不明白如何将它应用到我的脚本中。

function jakob_translate

figure('name','Jakob translate','color',[0.5  0.5 0.9]);  % some things to work with
a=uicontrol('style','edit','position',[85 250 400 30]);
d=uicontrol('style','pushbutton','position',[200 200 50 40],'string','översätt','callback',@rovarspraket);
b=uicontrol('style','text','position',[85 100 400 60]);

function rovarspraket (~,~)   % this function should read the input string from a, and keep each vocal as it is, but every consonant should be modified to 'consonant o consonant'.

    c=get(a,'string'); %getting data that I can use to make a for loop.
    e=length(c);
    for i=1:e
        text{i}=c(i:i);
    end

    for i=1:e
  text(i:i) ;

  if text{i}=='a'     % If statement to do what i want the function to do, keep vocals and modify consonants.
         text{i}='a';
     elseif text{i}=='e'
         text{i}='e';
     elseif text{i}=='y'
         text{i}='y';
     elseif text{i}=='u'
         text{i}='u';
     elseif text{i}=='i'
         text{i}='i';
     elseif text{i}=='o'
         text{i}='o';
     elseif text{i}=='å'
         text{i}='å';
     elseif text{i}=='ä'
         text{i}='ä';
     elseif text{i}=='ö'
         text{i}='ö';
     else 
         text{i}=[text{i} 'o' text{i}];
  end

     k=text;


end
     set(b,'string',k)   %set the string in b to display the modified string from a.
   end
end

2 个答案:

答案 0 :(得分:0)

找到答案!

这是命令

我正在寻找的cell2mat!

刚刚添加了一行

k=cell2mat(k)

之前的

set(b,'string',k)

答案 1 :(得分:0)

这对于正则表达式来说听起来很棒。你想用自己加上“o”加上自己替换不是元音集成员的每个字符:

整个翻译功能如下所示:

c=get(a,'string'); %getting data that I can use to make a for loop.
translated = regexprep(c, '([^aeiouåäö])', '$1o$1');
set(b, 'string', translated);

说明:[abcde]将匹配列表中的任何单个字母。添加^会取消该集合,这意味着它匹配集合中的任何字母。最后,括号“捕获”该字母的值,并使其可在替换文本中用作$1