Cell array with space between the ' and }

21 ビュー (過去 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});

カテゴリ

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by