필터 지우기
필터 지우기

textscan different number of floating digits

조회 수: 2 (최근 30일)
Cakil
Cakil 2022년 12월 20일
편집: Stephen23 2022년 12월 20일
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
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)
tbl = 14×8 table
Unix Timestamp UTC_Date_&_Time Mitteleuropäische Zeit Battery Temperature Dissolved Oxygen Dissolved Oxygen Saturation Q ______________ ___________________ ______________________ _______ ___________ ________________ ___________________________ _____ 1632844800 2021-09-28 16:00:00 2021-09-28 18:00:00 3.56 7.939 11.01 98.236 0.993 1632848400 2021-09-28 17:00:00 2021-09-28 19:00:00 3.55 5.235 11.655 97.16 0.992 1632852000 2021-09-28 18:00:00 2021-09-28 20:00:00 3.55 4.12 11.947 96.747 0.992 1632855600 2021-09-28 19:00:00 2021-09-28 21:00:00 3.55 3.363 12.171 96.605 0.991 1632859200 2021-09-28 20:00:00 2021-09-28 22:00:00 3.55 2.678 12.384 96.506 0.99 1632862800 2021-09-28 21:00:00 2021-09-28 23:00:00 3.54 2.3 12.513 96.517 0.99 1632866400 2021-09-28 22:00:00 2021-09-29 00:00:00 3.55 1.813 12.664 96.391 0.99 1632870000 2021-09-28 23:00:00 2021-09-29 01:00:00 3.54 1.621 12.716 96.277 0.989 1632873600 2021-09-29 00:00:00 2021-09-29 02:00:00 3.54 1.525 12.734 96.159 0.989 1632877200 2021-09-29 01:00:00 2021-09-29 03:00:00 3.54 1.396 12.797 96.291 0.989 1632880800 2021-09-29 02:00:00 2021-09-29 04:00:00 3.54 1.041 12.909 96.181 0.989 1632884400 2021-09-29 03:00:00 2021-09-29 05:00:00 3.54 -0.142 13.22 95.272 0.987 1632888000 2021-09-29 04:00:00 2021-09-29 06:00:00 3.54 -0.131 13.266 95.633 0.987 1632891600 2021-09-29 05:00:00 2021-09-29 07:00:00 3.54 0.145 13.215 96.015 0.987

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
ans = 1×3 cell array
{'Loggers_3340-106208_33.7m.txt'} {'Loggers_3340-267560_30.0m.txt'} {'Loggers_3340-270176_31.65m.txt'}
C = regexp({S.name},'\d+\.?\d*(?=m)','once','match')
C = 1×3 cell array
{'33.7'} {'30.0'} {'31.65'}
[~,X] = sort(str2double(C));
S = S(X);
{S.name} % right order
ans = 1×3 cell array
{'Loggers_3340-267560_30.0m.txt'} {'Loggers_3340-270176_31.65m.txt'} {'Loggers_3340-106208_33.7m.txt'}
  댓글 수: 1
Cakil
Cakil 2022년 12월 20일
Thank you, it is working perfectly. I stuck on one solution but this one is much more easier.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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
Cakil
Cakil 2022년 12월 20일
The double equal part is mistakenly written. Sorry for the confusion. It was supposed be ;
DOm{k}=readloggerstxt(['Loggers_','3340','-',num2str(idm3(k),'%6d'),'_',num2str(idm4(k),'%g\n'),'m.TXT'])
I will be removing the num2str part as I will be using the strings.
I understand the logic and it is supposed to work but I guess i need to change my reader file as well. Becuase I am receiving the error below when I swicthed to strings. Maybe I should combine it with DIR().
%Error in readloggerstxt (line 6)
%fid=fopen(fname,'r');
Thank you for your support.
Image Analyst
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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by