matlab字符串(2)

电脑杂谈  发布时间:2020-05-28 07:06:49  来源:网络整理

A = pi*1000*ones(1,5);
sprintf( %f \n %.2f \n %+.2f \n .2f \n 2.2f \n, A)
MATLAB

执行上述示例代码并获得以下结果-

ans =  3141.592654 
 3141.59 
 +3141.59 
      3141.59 
 000003141.59
Shell

字符串连接

创建脚本文件并在其中键入以下代码-

%cell array of strings
str_array = {red,blue,green, yellow, orange};
% Join strings in cell array into single string
str1 = strjoin(str_array, "-")
str2 = strjoin(str_array, ",")
MATLAB

执行上述示例代码并获得以下结果-

str1 = red-blue-green-yellow-orange
str2 = red,blue,green,yellow,orange
Shell

查找并替换字符串

创建脚本文件并在其中键入以下代码-

students = {Bara Ali, Neha Bhatnagar, ...
            Nonica Malik, Madhu Gautam, ...
            Nadhu Sharma, Bhawna Sharma,...
            Muha Ali, Reva Dutta, ...
            Tunaina Ali, Sofia Kabir};
% The strrep function searches and replaces sub-string.
new_student = strrep(students(8), Reva, Poulomi)
% Display first names
first_names = strtok(students)
MATLAB

执行上述示例代码并获得以下结果-

Trial>> students = {Bara Ali, Neha Bhatnagar, ...
            Nonica Malik, Madhu Gautam, ...
            Nadhu Sharma, Bhawna Sharma,...
            Muha Ali, Reva Dutta, ...
            Tunaina Ali, Sofia Kabir};
% The strrep function searches and replaces sub-string.
new_student = strrep(students(8), Reva, Poulomi)
% Display first names
first_names = strtok(students)
new_student =
  1×1 cell 数组
    {Poulomi Dutta}
first_names =
  1×10 cell 数组
  1 至 7 列
    {Bara}    {Neha}    {Nonica}    {Madhu}    {Nadhu}    {Bhawna}    {Muha}
  8 至 10 列
    {Reva}    {Tunaina}    {Sofia}
Shell

比较字符串

创建脚本文件并在其中键入以下代码-

str1 = This is test
str2 = This is text
if (strcmp(str1, str2))
 sprintf(%s and %s are equal, str1, str2)
else
 sprintf(%s and %s are not equal, str1, str2)
end
MATLAB

执行上述示例代码并获得以下结果-


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-224948-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 李湘
      李湘

      不明白的是号称民主国家的美国竟不顾天下苍生

    • 苏岗岗
      苏岗岗

      #杨洋2015金投赏##杨洋轻奢young#哈哈哈哈

    热点图片
    拼命载入中...