Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 用字符串绘制矩形_String_Matlab_Drawing - Fatal编程技术网

String 用字符串绘制矩形

String 用字符串绘制矩形,string,matlab,drawing,String,Matlab,Drawing,我有一个字符串,如下所示: A=面积-364320-364320 365640 365640 现在我想在MATLAB中画一个有刺的矩形。 我使用以下代码绘制一个矩形: rectangle('Position',[-364320 -364320 364320 364320],'FaceColor',[0 .9 .8]) 但我想用一根绳子画出来。请帮我解决这个问题。您不能直接使用,但可以使用和来拆分字符串并将其转换为数字。 然而,正如所指出的,最好使用 代码得到: A =' AREA ( -364

我有一个字符串,如下所示:

A=面积-364320-364320 365640 365640

现在我想在MATLAB中画一个有刺的矩形。 我使用以下代码绘制一个矩形:

rectangle('Position',[-364320 -364320 364320 364320],'FaceColor',[0 .9 .8])

但我想用一根绳子画出来。请帮我解决这个问题。

您不能直接使用,但可以使用和来拆分字符串并将其转换为数字。 然而,正如所指出的,最好使用

代码得到:

A =' AREA ( -364320 -364320 ) ( 365640 365640 ) ';
b=strsplit(A)
b =
  1×11 cell array
  Columns 1 through 8
    ''    'AREA'    '('    '-364320'    '-364320'    ')'    '('    '365640'
  Columns 9 through 11
    '365640'    ')'    ''
a_array=str2double(b([4 5 8 9]));
rectangle('Position',a_array,'FaceColor',[0 .9 .8])
请注意,如果您的字符串稍有不同,您可能需要查看b变量中是否有空格

使用str2num的原始代码:


不能直接使用,但可以使用和拆分字符串并将其转换为数字。 然而,正如所指出的,最好使用

代码得到:

A =' AREA ( -364320 -364320 ) ( 365640 365640 ) ';
b=strsplit(A)
b =
  1×11 cell array
  Columns 1 through 8
    ''    'AREA'    '('    '-364320'    '-364320'    ')'    '('    '365640'
  Columns 9 through 11
    '365640'    ')'    ''
a_array=str2double(b([4 5 8 9]));
rectangle('Position',a_array,'FaceColor',[0 .9 .8])
请注意,如果您的字符串稍有不同,您可能需要查看b变量中是否有空格

使用str2num的原始代码:

您可以使用带有“match”选项的regexp直接提取数字,如下所示

% Extract strings which match:
%    -?  means 0 or 1 - signs
%    \d+ means 1 or more digits
% Using the 'match' argument returns the matching strings, rather than their indices
% Use str2double to convert from array of strings to numerical array
B = str2double(regexp(A, '-?\d+', 'match'));
% Create the rectangle
rectangle('Position', B, 'FaceColor', [0 .9 .8])
最好使用str2double而不是str2num,因为它不在引擎盖下使用eval

此方法只需选择数值,而不考虑字符串的格式。

您可以使用带有“匹配”选项的regexp直接提取数字,如下所示

% Extract strings which match:
%    -?  means 0 or 1 - signs
%    \d+ means 1 or more digits
% Using the 'match' argument returns the matching strings, rather than their indices
% Use str2double to convert from array of strings to numerical array
B = str2double(regexp(A, '-?\d+', 'match'));
% Create the rectangle
rectangle('Position', B, 'FaceColor', [0 .9 .8])
最好使用str2double而不是str2num,因为它不在引擎盖下使用eval


这个方法只会选择数值,而不管字符串的格式。

不知道str2num的问题,所以我相应地调整了答案@每天都要学习新的东西!来自:str2num函数不转换字符串或单元格数组,并且对+和-运算符周围的间距敏感。此外,str2num使用eval函数,当输入包含函数名时,该函数可能会导致意外的副作用。为了避免这些问题,请使用str2double。我不知道str2num的问题,所以我相应地调整了我的答案@每天都要学习新的东西!来自:str2num函数不转换字符串或单元格数组,并且对+和-运算符周围的间距敏感。此外,str2num使用eval函数,当输入包含函数名时,该函数可能会导致意外的副作用。要避免这些问题,请使用str2double。