textscan different number of floating digits
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-12-20
编辑:Stephen23
2022-12-20
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)
采纳的回答
Stephen23
2022-12-20
编辑:Stephen23
2022-12-20
"...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
更多回答(1 个)
Image Analyst
2022-12-20
编辑:Image Analyst
2022-12-20
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 个评论
Image Analyst
2022-12-20
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. 🙂
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!