필터 지우기
필터 지우기

Reading a text file containing strings.

조회 수: 1 (최근 30일)
Dhanalakshmi M
Dhanalakshmi M 2021년 3월 22일
댓글: Dhanalakshmi M 2021년 3월 24일
I have a data recorded from a sensor every day which is listed below:
34312810010110000000000000000000000000000000000000000000000000000000000000000 34312810010210000000000000000000000000000000000000000000000000000000000000000 34312810010310000000000000000000000000000000000000000000000000000000000000000 34312810010410000000000000000000000000000000000000000000000000000000000000000 34312810010510000000000000000000000000000000000000000000000000000000000000000 34312810010610000000000000000000000000000000000000000000000000000000000000000 34312810010710000000000000000000000000000000000000000000000000000000000000000 34312810010810000000000000000000000000000000000000000000000000000000000000000 34312810010910000000000000000000000000000000000000000000000000000000000000000 34312810011010000000000000000000000000000000000000000000000000000000000000000 34312810011110000000000000000000000000000000000000000000000000000000000000000
I have written a code for interpreting the data as follows:
str = '34312810010110000000000000000000000000000000000000000000000000000000000000000'
D1=str(1) % Element Indicator
D2=str(2:6) % Index number of the station
D3=str(7:8) % Year
D4=str(9:10) % Month
D5=str(11:12) % Date
D6=str(13) % Record Number
and so on
D7
:
:
etc as upto the end of the first line and later I convert these strings to numbers using strl2num.
I am able to process the data for one line with this code, how to do the same process repeadedly for 'n' number of lines keeping the whole data in a text file.
Thanks in advance.
Lakshmi.

채택된 답변

Star Strider
Star Strider 2021년 3월 22일
편집: Star Strider 2021년 3월 22일
It would have been appropriate to include the file you want to read. Lacking that, I created my own version (‘Dhanalakshmi M 20210322.txt’, attached) to test this.
The Code —
opts = detectImportOptions('Dhanalakshmi M 20210322.txt');
VarNames = {'ElementIndicator','StationIndexNumber','Year','Month','Day','RecordNr','AndSoOn'};
VarWidths = [1, 6, 2, 2, 2, 1, 77-14];
VarTypes = {'int64','double','int64','int64','int64','int64','int64'};
opts = fixedWidthImportOptions('NumVariables',7, 'VariableNames',VarNames, 'VariableWidths',VarWidths, 'VariableTypes',VarTypes);
Mread = readtable('Dhanalakshmi M 20210322.txt', opts);
Mread.StationIndexNumber = Mread.StationIndexNumber * 1E+5;
The Result —
Mread =
11×7 table
ElementIndicator StationIndexNumber Year Month Day RecordNr AndSoOn
________________ __________________ ____ _____ ___ ________ _______
3 43128 10 1 1 1 0
3 43128 10 1 2 1 0
3 43128 10 1 3 1 0
3 43128 10 1 4 1 0
3 43128 10 1 5 1 0
3 43128 10 1 6 1 0
3 43128 10 1 7 1 0
3 43128 10 1 8 1 0
3 43128 10 1 9 1 0
3 43128 10 1 10 1 0
3 43128 10 1 11 1 0
For some reason, ‘StationIndexNumber’ was imported with a leading decimal point. I solved that by simply multiplying it by . Perhaps not the most elegant solution, however it has the advantage of producing the correct result.
Make appropriate changes to get the result you want.
EDIT — Corrected typographical errors.
  댓글 수: 7
Star Strider
Star Strider 2021년 3월 23일
My code as I posted it ran for me without error in R2021a.
The readtable function was introduced in R2013b, however detectImportOptions was introduced in R2016b and fixedWidthImportOptions (function) in R2018b.
It will be necessary to upgrade to at least R2018b (and preferably to R2021a) to run my code. I no longer have access to R2014b (or even its documentation), so I have no idea currently how to work around that limitation to parse your file.
I will see if I can come up with something using the fileread function, although it will not be as efficient as the code I posted.
Dhanalakshmi M
Dhanalakshmi M 2021년 3월 24일
Sir, Thanks for your kind reply. I have altered other lines as per the R2014a version, but I am getting error on this line only SensorData = readtable(filename, opts); I am trying to solve this error, it is giving wrong number of arguments. Thanks once again for your help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by