Read mixed numbers in Matlab
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John Smith
el 22 de Dic. de 2018
Comentada: Stephen23
el 30 de Dic. de 2018
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 comentarios
Respuesta aceptada
Stephen23
el 23 de Dic. de 2018
Editada: Stephen23
el 23 de Dic. de 2018
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 comentarios
Stephen23
el 30 de Dic. de 2018
@John Smith: that is quite a different topic. Please ask a new question about that.
Más respuestas (2)
Brian Hart
el 22 de Dic. de 2018
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 comentario
Guillaume
el 22 de Dic. de 2018
Editada: Guillaume
el 24 de Dic. de 2018
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 comentarios
Guillaume
el 23 de Dic. de 2018
Editada: Guillaume
el 23 de Dic. de 2018
Sorry,forgot the input to cellfun. Fixed now.
In the future, please copy/paste the error as text rather than a screenshot.
Note: the advantage of my answer over the other proposed ones, is that it will parse correctly
28 21/64
21/64
28
Guillaume
el 24 de Dic. de 2018
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.
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!