Convert cell array to double, with comma separated elements

조회 수: 20 (최근 30일)
France
France 2020년 3월 18일
편집: Guillaume 2020년 3월 19일
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
Guillaume
Guillaume 2020년 3월 18일
"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 2020년 3월 18일
thank you. what about double?

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2020년 3월 18일
>> 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
Guillaume
Guillaume 2020년 3월 18일
@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 2020년 3월 18일
ok..thanks for the patient!

댓글을 달려면 로그인하십시오.


Sajeer Modavan
Sajeer Modavan 2020년 3월 18일
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
dpb
dpb 2020년 3월 19일
More better to make your code fit the OPs data... :)
Guillaume
Guillaume 2020년 3월 19일
편집: Guillaume 2020년 3월 19일
"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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by