textscan different number of floating digits
Mostrar comentarios más antiguos
Hello Matlab Community,
I have a dataset with 'Loggers_3340_837584_20.5m_TXT' format.
For example:
'Loggers_3340_838653_20.55m_TXT
'Loggers_3340_200584_30.0m_TXT
'Loggers_3340_636400_23.5m_TXT
'Loggers_3340_268584_12.5m_TXT
Bold parts are changing in each file.
I am using below script to read files in loop. My problem is changing decimal digits in depth (xx.xm or xx.xxm) . I put only the reading part of the script to get your opinions.
If I use '%g\n', the files ending with one digit zero are read as 30m instead of 30.0m.
But if I use '%0.1f' or '%0.2f', the output is whether two or one decimal digits which does not work in my case.
DOm{iii}=readloggerstxt(['Loggers_','3340','-',num2str(idm3(iii),'%6d'),'_',num2str(idm4(iii),'%g\n'),'m.TXT'])
What can be an alternative way to read varying decimal parts?
Thank you in advance!
1 comentario
You should use READTABLE() to get the correct variable types, e.g.:
fnm = 'Loggers_3340-106208_33.7m.txt';
obj = detectImportOptions(fnm, 'filetype','text', 'delimiter',',', ...
'VariableNamingRule','preserve', 'VariableNamesLine',8, 'VariableUnitsLine',9);
obj = setvartype(obj, 'Unix Timestamp','uint64');
tbl = readtable(fnm, obj)
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 20 de Dic. de 2022
Editada: Image Analyst
el 20 de Dic. de 2022
Did you try using dir?
fileList = dir('Loggers*.txt');
allFileNames = {fileList.name}; % Get all filenames into a cell array.
for iii = 1 : numel(allFileNames)
% Read results into the two arrays.
DOm{iii} = readloggerstxt(allFileNames{iii});
readloggerstxtDOm{iii} = readloggerstxt(allFileNames{iii});
% Now, here comes the calculations
end
6 comentarios
Cakil
el 20 de Dic. de 2022
Image Analyst
el 20 de Dic. de 2022
Editada: Image Analyst
el 20 de Dic. de 2022
I am totally baffled. I just can't believe that works. For example, this simplified version throws an error:
readloggerstxtDOm = cell(1);
DOm = cell(1)
iii = 1;
% Call it with two equals signs.
DOm{iii}=readloggerstxtDOm{iii}=readloggerstxt('blah')
% Define a simplified function
function output = readloggerstxt(str)
output = 1;
Not sure what you want but I would have done one of these below. However it looks like you say Stephen's solution works.
idm3=[267560 270176 106208];
idm4=[30.0 31.65 33.7];
% Process the files in this specific, special order. idm3 and idm4 are synced up.
for k = 1: numel(idm3)
% Construct filename. Might want %6.6d if you want leading zeros to appear
filename = sprintf('Loggers_3340-%6d_%gm.TXT', idm3(k), idm4(k));
fprintf('Processing %s\n', filename);
if isfile(filename)
% Process it. Can't believe the line below will work though.
% DOm{k}=readloggerstxtDOm{k}=readloggerstxt(])
%here comes the calculations
else
fprintf(' but %s does not exist!\n', filename);
end
end
fprintf('\n=================================================\n')
% Or maybe you want all combinations like this:
% Process the files in this specific, special order.
for k1 = 1: numel(idm3)
for k2 = 1: numel(idm4)
% Construct filename. Might want %6.6d if you want leading zeros to appear
filename = sprintf('Loggers_3340-%6d_%gm.TXT', idm3(k1), idm4(k2));
fprintf('Processing %s\n', filename);
if isfile(filename)
% Process it. Can't believe the line below will work though.
% DOm{k}=readloggerstxtDOm{k}=readloggerstxt(])
%here comes the calculations
else
fprintf(' but %s does not exist!\n', filename);
end
end
end
Cakil
el 20 de Dic. de 2022
Image Analyst
el 20 de Dic. de 2022
Then you can define them as strings instead of numbers and there won't be any problem.
idm3={'267560', '270176', '106208'};
idm4={'30.0', '31.65', '33.7'};
% Process the files in this specific, special order. idm3 and idm4 are synced up.
for k = 1: numel(idm3)
% Construct filename. Might want %6.6d if you want leading zeros to appear
filename = sprintf('Loggers_3340-%s_%sm.TXT', idm3{k}, idm4{k});
fprintf('Processing %s\n', filename);
if isfile(filename)
% Process it. Can't believe the line below will work though.
% DOm{k}=readloggerstxtDOm{k}=readloggerstxt(])
%here comes the calculations
else
fprintf(' but %s does not exist!\n', filename);
end
end
Results:
Processing Loggers_3340-267560_30.0m.TXT
Processing Loggers_3340-270176_31.65m.TXT
Processing Loggers_3340-106208_33.7m.TXT
Cakil
el 20 de Dic. de 2022
Image Analyst
el 20 de Dic. de 2022
OK, good. I always think it's easier to read if you use sprintf() to construct the filename instead of the bracket and num2str way.
With both the way you made the filename, and the way I did, both ways sent a string into readloggerstxt() so if it's not working, you'll have to figure out why readloggerstxt() does not like a string.
And glad you finally understand what I was saying about the double = giving a syntax error.
You can only "Accept" one answer but you can "Vote" for as many as you want. If I helped you, can you click the thumbs up Vote icon. Accepting or Voting for an answer will award the answerer with "reputation points" for their efforts in helping you. They'd appreciate it. Thanks in advance. 🙂
Categorías
Más información sobre Environment and Settings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!