Read mixed numbers in Matlab
조회 수: 3 (최근 30일)
이전 댓글 표시
Dear Sir/Madam,
I have a data file containing text and mixed number like this: (see the file attached data.txt.)
AA, BB, 28 21/64, 28 45/64,
AA, BB, 1/64, 11/64,
the mixed number have format:
integer space numerator/denominator
I would like to read the data file in matlab as
AA, BB, 28.328125, 28.703125,
AA, BB, 0.015625, 0.171875,
i. e. read mixed numbers and convert them into decimal numbers.
What Matlab command to use? I would greatly appreciate it if you left your code and running output.
I am using MATLAB R2014a.
Thank you
댓글 수: 0
채택된 답변
Stephen23
2018년 12월 23일
편집: Stephen23
2018년 12월 23일
opt = {'CollectOutput',true,'Delimiter',','};
fmt = repmat('%s',1,4);
[fid,msg] = fopen('data.txt','rt');
assert(fid>=3,msg);
C = textscan(fid,fmt,opt{:})
fclose(fid);
C = C{1}; % raw character data in a cell array.
% Convert fractions to numeric:
foo = @(v) sum([v(1:end-2),v(end-1)/v(end)]);
baz = @(s) foo(sscanf(s,'%d%*[ /]'));
M = cellfun(baz,C(:,3:4))
Giving:
M =
28.328125 28.703125
0.031250 0.046875
and the string data is in C. If you want the numeric data reinserted back into C then use num2cell:
>> C(:,3:4) = num2cell(M)
C =
'AA' 'CC' 28.328125 28.703125
'AA' 'CC' 0.031250 0.046875
댓글 수: 14
Stephen23
2018년 12월 30일
@John Smith: that is quite a different topic. Please ask a new question about that.
추가 답변 (2개)
Brian Hart
2018년 12월 22일
Here's some code to read the data in as a table, then update the mixed number values in each row to the decimal...
T = readtable("C:\Users\MATLAB\Desktop\data.txt",'Delimiter',',','ReadVariableNames',false)
numRows = size(T,1);
for i = 1:numRows
for j=3:4
tmpstr = T{i,j};
tmpstr=tmpstr{:};
tmpstr=strrep(tmpstr, '/',' ');
mixedVal = sscanf(tmpstr,'%d');
if size(mixedVal,1) == 2
decVal = mixedVal(1)/mixedVal(2);
else
decVal = mixedVal(1) + mixedVal(2)/mixedVal(3);
end
T(i,j) = {num2str(decVal)};
end
end
disp(T)
Guillaume
2018년 12월 22일
편집: Guillaume
2018년 12월 24일
I would recommend that you modify whatever is creating these files so that it creates files that don't have such an unusual format.
As it is, the following should work but is not particularly good code:
data = readtable('data.txt', 'Delimiter', ',', 'ReadVariableNames', false);
data = [data(:, [1 2]), ... %leave text variables unchanged
varfun(@(var) cellfun(@(var) str2num(strrep(var, ' ', '+')), var), data, 'InputVariables', [3 4])]; %convert 'numeric' variables
It simply replaces the space by + and use num2str to parse the expression which is simple but dangerous.
edited to add missing input to cellfun and wrong function use
댓글 수: 3
Guillaume
2018년 12월 24일
John wrote in a comment now deleted: "Could you please make your code also apply to decimal numbers (as well as mixed number). The reason is that some data has mixed numbers, some data use decimal numbers. I would like have one code fits all. "
As I pointed out, my answer already does that. In a much simpler way. Also note that the numeric columns are stored as vectors which is more memory efficient and easier to use than a cell array of scalars.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!