简体   繁体   English

CAT参数:尺寸不一致

[英]CAT arguments: dimensions are inconsistent

Kindly look at this MATLAB code: 请看下面的MATLAB代码:

formatspec = '%5i';
delay = num2str(d,formatspec);
datasave = [datasave; repeating_character blanks(5) num2str(lenmax) blanks(5) delay];
end

I want to display the result datasave in the form of a table. 我想要显示的结果datasave以表格的形式。 The variable d value will be incremented from 2 to 127 in a for loop. 在for循环中,变量d值将从2增加到127 There is no problem with lenmax variable since it is always a single digit number. lenmax变量没有问题,因为它始终是一个数字。 But the problem is with variable delay . 但是问题在于可变的delay In the first instance the value of delay = 2 , since the loop starts with 2 , a specific dimension will be assigned to datasave . 在第一种情况下, delay = 2的值,由于循环从2开始,因此将为datasave分配一个特定的维。 Then the value of d keeps on incrementing in the loop. 然后, d的值在循环中继续增加。 During the 9 th instance, it will become 10 which is a 2 digit integer. 在第9 实例中,它将变为10 ,这是一个2位数的整数。 The problem is here. 问题在这里。 When it becomes a 2-digit interger (10), the dimensions will not match and error using vertcat comes out since the dimensions are not same. 当它变成两位数的整数(10)时,尺寸将不匹配,并且由于尺寸不相同,将出现使用vertcat错误。

I thought using %5d should solve the problem, but it doesn't. 我认为使用%5d应该可以解决问题,但事实并非如此。 If I change loop such that it starts from 10 to 127 , the problem appears when delay = 100 . 如果将循环从10开始更改为127 ,则当delay = 100时出现问题。 The values from 10 to 99 will be displayed but while displaying 100 , same error about dimension mismatch pops up. 将显示从1099的值,但是在显示100 ,会弹出有关尺寸不匹配的相同错误。

Please can anyone tell me how to solve this problem? 请谁能告诉我如何解决这个问题?

As @RodyOldenhuis explained, I think it's a subtle bug in numstr . 正如@RodyOldenhuis解释的那样,我认为这是numstr的一个细微错误。

One ways to work around this is to pass the whole vector to num2str at once, that way white spaces are not trimmed (it's still getting trimmed, but no more than what's allowed by the longest string): 解决此问题的一种方法是立即将整个向量传递给num2str ,这样就不会修剪空格(它仍在修剪中,但不超过最长字符串所允许的范围):

>> delays = num2str((2:127)', '%3d');
>> whos delays
  Name          Size            Bytes  Class    Attributes

  delays      126x3               756  char    

>> delays([1 end],:)
ans =
  2
127

You could also choose to pad with zeros instead of spaces: 您还可以选择用零而不是空格填充:

>> num2str(2, '%05d')
ans =
00002

You could also use the undocumented sprintfc (which returns a cell-array of untrimmed strings): 您还可以使用未记录的sprintfc (它返回未sprintfc字符串的单元格数组):

>> sprintfc('%5d', (2:127)')
ans = 
    '    2'
         .
         .
    '  127'

If you look at edit num2str , at the bottom of the main function, you'll see this (or something similar, undoubtedly depending on MATLAB version): 如果您在主函数底部查看edit num2str ,您会看到以下内容(或类似的内容,这无疑取决于MATLAB版本):

s = strtrim([cols{:}]);

This basically means that any white space you explicitly put in using the formatspec gets erased; 基本上,这意味着您使用formatspec显式放入的所有空白都会被删除; a bug if you ask me. 如果您问我一个错误。

It's easiest to use char to do the concatentation: 使用char进行组合最简单:

datasave = char(datasave, [repeating_character blanks(5) num2str(lenmax) blanks(5) delay]);

Alternatively, you can use cellstrings : 另外,您可以使用cellstrings

%# in the loop
%# (better to do this with pre-allocation) 
datasave{end+1} = [...
    repeating_character,...
    blanks(5),...
    num2str(lenmax),... 
    blanks(5),...
    delay];

...

%# after the loop
datasave = char(datasave);

Alternatively, you can use int2str : 另外,您可以使用int2str

delay = int2str((2:127).')

Alternatively, you can use sprintf : 另外,您可以使用sprintf

formatspec = '%5i\n';
delay = sprintf(formatspec, 2:127)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM