简体   繁体   中英

CAT arguments: dimensions are inconsistent

Kindly look at this MATLAB code:

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. The variable d value will be incremented from 2 to 127 in a for loop. There is no problem with lenmax variable since it is always a single digit number. But the problem is with variable delay . In the first instance the value of delay = 2 , since the loop starts with 2 , a specific dimension will be assigned to datasave . Then the value of d keeps on incrementing in the loop. During the 9 th instance, it will become 10 which is a 2 digit integer. 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.

I thought using %5d should solve the problem, but it doesn't. If I change loop such that it starts from 10 to 127 , the problem appears when delay = 100 . The values from 10 to 99 will be displayed but while displaying 100 , same error about dimension mismatch pops up.

Please can anyone tell me how to solve this problem?

As @RodyOldenhuis explained, I think it's a subtle bug in 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):

>> 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('%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):

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

This basically means that any white space you explicitly put in using the formatspec gets erased; a bug if you ask me.

It's easiest to use char to do the concatentation:

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

Alternatively, you can use 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 :

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

Alternatively, you can use sprintf :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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