Hi,
I am using the example below to split cells and it works good. However, when I use it, it rounds values like 0.00178 to 0.0018. How can I do the same thing, but then keeping all the decimals?
I'm using this code in a 100031x1 cell and I get a double with rounded values
a={'0,0,0,2,2,0.39,0.49'
'0,1,2,2,2,0.34,0.44'}
out=cell2mat(cellfun(@str2num,strrep(a,',',' '),'un',0))

댓글 수: 3

The values are not actually being rounded. It appears they are when visualizing out in the command window. If you actually check the values contained in out you will find that the values are correct.
For visualizing the full number in the command window, try to run
format long
before executing your code. This will change the format used for display in the command window.
Stephen23
Stephen23 2018년 6월 6일
"However, when I use it, it rounds values like 0.00178 to 0.0018"
I doubt that any rounding is occurring. How values are displayed is a totally different thing to what values are stored in memory. Try changing the format.
Thijs Obers
Thijs Obers 2018년 6월 6일
yes, you are right, thank you!

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

 채택된 답변

Stephen23
Stephen23 2018년 6월 6일
편집: Stephen23 2018년 6월 6일

0 개 추천

There is no rounding occurring. Change the format to precision with which values are displayed, e.g.:
format long
Also note that you can make this conversion more efficiently and securely using sscanf (because unfortunately str2num hides an eval call inside):
>> a = {'0,0,0,2,2,0.39,0.49';'0,1,2,2,2,0.34,0.44'};
>> C = cellfun(@(s)sscanf(s,'%f,',[1,Inf]),a,'uni',0);
>> vertcat(C{:})
ans =
0 0 0 2 2 0.39 0.49
0 1 2 2 2 0.34 0.44

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2018년 6월 6일

댓글:

2018년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by