Convert cell array to double, with comma separated elements

19 visualizaciones (últimos 30 días)
France
France el 18 de Mzo. de 2020
Editada: Guillaume el 19 de Mzo. de 2020
Hello guys,
I have got a cell array like the follow:
'1.265000e+02, 2.505000e+02, 22, 27, '
'1.275000e+02, 2.505000e+02, 20, 29, '
'1.275000e+02, 2.525000e+02, 20, 25, '
...
I need to convert it in double (or uint8). The outup should be:
[126.5, 250.5, 22, 27]
What I need is that all the values must be in a single cell and that a comma separeted any value. The last comma from any cell should be removed.
L'output in double/ uint8
Can you please help me? Many thanks in advance!!
  2 comentarios
Guillaume
Guillaume el 18 de Mzo. de 2020
"I need to convert it in double (or uint8)"
I doubt you want uint8 (8-bit integer). 126.5 is not an integer so will be rounded. With matlab's strange integer conversion rule, this would be rounded to 127. In addition, the maximum value that can be stored as uint8 is 255, very close to your 250.5.
France
France el 18 de Mzo. de 2020
thank you. what about double?

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 18 de Mzo. de 2020
>> v=cell2mat(cellfun(@(s) sscanf(s,'%f,').',c,'UniformOutput',false))
v =
126.5000 250.5000 22.0000 27.0000
127.5000 250.5000 20.0000 29.0000
127.5000 252.5000 20.0000 25.0000
>>
However, it would probably be better to read the data in numerically instead...how did you obtain the above cell content?
  12 comentarios
Guillaume
Guillaume el 18 de Mzo. de 2020
@Frances, there's a huge difference between what matlab displays (in the variable browser and at the command prompt) and what matlab stores. For example, look at the following table in the variable browser and command prompt
>> t = table([1;2;3], {[1 2 3]; [4, 5, 6, 7]; []})
t =
3×2 table
Var1 Var2
____ ____________
1 {1×3 double}
2 {1×4 double}
3 {0×0 double}
Despite, the variable browser showing brackets and commas, these are not part of the data. They're just display elements to show you that each row of Var2 is a vector of numbers.
In your case, as the help file you linked states, the remaining columns must be a cell vector of Mx4 (actually 1x4) matrices. To create that from your bounding boxes:
%...
props = regionprops(image, 'BoundingBox');
BoundingBoxes = num2cell(vertcat(props.BoundingBox), 2); %vertically concatenate the bounding boxes into a Mx4 matrix, then split into a Mx1 cell array or 1x4 matrix
France
France el 18 de Mzo. de 2020
ok..thanks for the patient!

Iniciar sesión para comentar.


Sajeer Modavan
Sajeer Modavan el 18 de Mzo. de 2020
I hope you are looking this
A = ['1.265000e+02, 2.505000e+02, 22, 27, '
'1.275000e+02, 2.505000e+02, 20, 29, '
'1.275000e+02, 2.525000e+02, 20, 25, '];
str2num(A(1,:))
  6 comentarios
dpb
dpb el 19 de Mzo. de 2020
More better to make your code fit the OPs data... :)
Guillaume
Guillaume el 19 de Mzo. de 2020
Editada: Guillaume el 19 de Mzo. de 2020
"your cell array is not like what I wrote"
You did not write a cell array! Your A is a 2D char vector. That's very different. The cell array would be:
A = {'1.265000e+02, 2.505000e+02, 22, 27, ';
'1.275000e+02, 2.505000e+02, 20, 29, ';
'1.275000e+02, 2.525000e+02, 20, 25, '};
Note the use of {} not [].
"if you make it in this format, then it will work sure,"
Except it is impossible to construct a 2D char vectors with rows of different length:
>> A = ['1.265000e+02, 2.505000e+02, 22, 27.5, '; %<--- longer row here
'1.275000e+02, 2.505000e+02, 20, 29, ';
'1.275000e+02, 2.525000e+02, 20, 25, '];
Error using vertcat
Dimensions of arrays being concatenated are not
consistent.
whereas it's not an issue with cell arrays:
>> A = {'1.265000e+02, 2.505000e+02, 22, 27.5, '; %<--- longer row here
'1.275000e+02, 2.505000e+02, 20, 29, ';
'1.275000e+02, 2.525000e+02, 20, 25, '}; %does not generate an error

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by