Changing content between 2 tab delimiters
조회 수: 3 (최근 30일)
이전 댓글 표시
Dear community,
I have many txt files that uses a tab delimiter. I have to change the content between the 17th and 18th tab delimiter of all my files. This is one the data inside the file:
_______
Data file generated on Tue Aug 30 12:40:29 2022
A B C D E F G H I J K L M N O P Q R S T U
1 2.00000 2.00000 0.00000 1.00000 28.00000 64.00000 88.00000 1.20000 275.00000 50.00000 30.00000 165.00000 850.00000 500.00000 0.00000 10.00000 10.00000 15.00000 0,0,0,0, 0,0,0,0, false
_______
So I have to change the value for the variable Q (in this case 10.0000) for a specific value. However, some of my files have a different pattern for numbers, instead of 10.00000 it is only 10, what makes the counting characters unfeasible. Furthermore, after some delimiters there are spaces. Then, I decided to count the tab delimeters, and change the content between the 17th and 18th delimiter to solve my problem, that I don't know if it will work. This is my code so far...
A = regexp(fileread(myfile), '\n', 'split'); %upload my file to a cell array
B = strfind(A{4}, sprintf('\t')); %takes the cell 4 in the array and finds where I have tab delimiters
C = B{4}(17)+1:B{4}(18)-1; %select the content between the 17th and 18th tab delimiters
Please, can somebody help me???
댓글 수: 0
채택된 답변
Cris LaPierre
2022년 10월 2일
I'd use readtable to load the data into MATLAB, and then make the changes to variable Q. See the Access Data in Tables doc page. If you need to get the table back into a text file, use writetable.
d = readtable('myfile.txt')
d.Q(1) = 11
댓글 수: 6
Cris LaPierre
2022년 10월 3일
편집: Cris LaPierre
2022년 10월 3일
Off hand, I can't think of a reason why that is happening. Could you share the original file where that is happening for me to test? You can attach it using the paperclip icon.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!