How can I set the FormatSpec to read this numerical value?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi all,
I have some some difficulties to read from file the following numerical value by using textscan
'-1.661-4'
The problem lies in the lack of the exponential character that makes Matlab read -1.661.
Is there a simple way to set a FormatSpec to solve the problem. I tried with 'ExpChar ','' but it didn't work.
PS: this numerical value is within a loop where just few values have this structure.
댓글 수: 0
채택된 답변
dpb
2018년 12월 7일
ML doesn't have the facility built into any of the text conversion routines to handle the omitted exponent indicator; some Fortran compilers have that as an extension to allow such a format.
One way if the file is regular in there always being the two elements is to write a little processing function...
s='-1.661-4,3.141+0'; % more general input string example
v=reshape(cell2mat(textscan(s,'%f','delimiter',',')),2,[]).'; % parse fields to mantiss/exponent
v=v(:,1).*10.^v(:,2); % convert to value
>> fprintf('%g %g\n',v) % show what we got
-0.0001661 3.141
>>
If there are missing elements in some entries, then a parsing of each element and interpretation on a field-by-field basis would be necessary or write a more intelligent regular expression substitution pattern to correctly insert the missing exponent indication letter where it is needed.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Adding custom doc에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!