Read mixed numbers in Matlab
7 ビュー (過去 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)
1 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!