Cell array with space between the ' and }

조회 수: 5 (최근 30일)
James Browne
James Browne 2022년 9월 5일
편집: Stephen23 2022년 9월 5일
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