Cell array with space between the ' and }

4 次查看(过去 30 天)
Hi
I have been writing some code with reg expressions that reads from a text file. It has been working with most of the text files, except that when it read in:
Data=(350.0,15.0)
I got a cell array that behaved strangely:
-----------------------
2×1 cell array
{'350.0'}
{'15.0' }
------------------------
This cell array wont allow me to do cell2mat saying:
---------------------------
Error using cat
Dimensions of arrays being concatenated are not consistent.
---------------------------
As far as I can see, the only strange thing about the cell array is that the {'15.0' } has a space after the ' and before the }.
Is this the cause of not being able to do the cell2mat function? If so, how can I fix it?
  1 个评论
Torsten
Torsten 2022-9-5
编辑:Torsten 2022-9-5
a = [{'350.0'};{'15.0' }]
a = 2×1 cell array
{'350.0'} {'15.0' }
class(a)
ans = 'cell'
a = str2double(a)
a = 2×1
350 15
class(a)
ans = 'double'

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2022-9-5
编辑:Stephen23 2022-9-5
"the only strange thing about the cell array is that the {'15.0' } has a space after the ' and before the }."
It is not strange at all: character vectors of different lengths within one cell array are displayed with their exact length (of course), whereas the cell array curly braces are displayed aligned (for aesthetic reasons?). So those spaces are purely an artifact of how the cell array is displayed, they makes absolutely no difference to the data itself:
C = {'hello world';'x';'the quick brown fox jumps over the lazy dog'}
C = 3×1 cell array
{'hello world' } {'x' } {'the quick brown fox jumps over the lazy dog'}
"Is this the cause of not being able to do the cell2mat function?"
No, that space is purely an irrelevent display artifact. As the error message clearly states, the two character vectors that you are trying to vertically concatenate have inconsistent dimensions: your first character vector has five characters, whereas the second one has only four characters in it. What do you expect MATLAB to do, if you try to vertically concatenate two arrays which have a different number of columns? The error messages tell you, that your array sizes are not compatible, so you should start to debug by looking at your array sizes.
It is not clear what you expect the output to be (you did not explain this important information in your question, simply showing some buggy code is not a replacement for your explanation of what you are actually trying to achieve), so here are some guesses:
C = {'350.0';'15.0'}
C = 2×1 cell array
{'350.0'} {'15.0' }
V = str2double(C) % convert to numeric vector
V = 2×1
350 15
M = char(C{:}) % convert to char matrix
M = 2×5 char array
'350.0' '15.0 '
S = string(C) % convert to string vector
S = 2×1 string array
"350.0" "15.0"
cell2mat(C) % will not work if the arrays have incompatible sizes!
Error using cat
Dimensions of arrays being concatenated are not consistent.

Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by