Cell array with space between the ' and }
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponses (1)
Stephen23
le 5 Sep 2022
Modifié(e) : Stephen23
le 5 Sep 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'}
"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'}
V = str2double(C) % convert to numeric vector
M = char(C{:}) % convert to char matrix
S = string(C) % convert to string vector
cell2mat(C) % will not work if the arrays have incompatible sizes!
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!