Cell array with space between the ' and }

4 visualizaciones (últimos 30 días)
James Browne
James Browne el 5 de Sept. de 2022
Editada: Stephen23 el 5 de Sept. de 2022
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 comentario
Torsten
Torsten el 5 de Sept. de 2022
Editada: Torsten el 5 de Sept. de 2022
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'

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 5 de Sept. de 2022
Editada: Stephen23 el 5 de Sept. de 2022
"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});

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by