textscan different number of floating digits
1 view (last 30 days)
Show older comments
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 Comment
Stephen23
on 20 Dec 2022
Edited: Stephen23
on 20 Dec 2022
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)
Accepted Answer
Stephen23
on 20 Dec 2022
Edited: Stephen23
on 20 Dec 2022
"...but I need to order them according to the last part which is 30.0m 31.65m 33.5m"
So why not just sort the filenames? That is very easy to do:
P = '.'; % absolute or relative path to where the files are
S = dir(fullfile(P,'Loggers*.txt'));
{S.name} % wrong order
C = regexp({S.name},'\d+\.?\d*(?=m)','once','match')
[~,X] = sort(str2double(C));
S = S(X);
{S.name} % right order
More Answers (1)
Image Analyst
on 20 Dec 2022
Edited: Image Analyst
on 20 Dec 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 Comments
Image Analyst
on 20 Dec 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. 🙂
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!