String 格式化循环生成的字符串

String 格式化循环生成的字符串,string,matlab,loops,text,String,Matlab,Loops,Text,我要做的是从edit获取字符串,然后通过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

我要做的是从edit获取字符串,然后通过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
找到了鞍衣

这是命令

我在找的那个人

刚刚加了一行

k=cell2mat(k)
之前在线

set(b,'string',k)

这听起来像是正则表达式的一个很好的应用程序。您希望将不是元音集成员的每个字符替换为自身加上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用于替换文本。

通过在b['sos';'e';'dod';'e';'sos']a列中键入一个'sedes',我将获得它。你应该得到什么?我不太清楚你想要实现什么。你能给你的代码添加注释吗?我在代码中做了一些注释来编辑代码。代码可以工作,但我想在b中将answear显示为单行字符串。现在,每个字母都在一个新的行中。