Changing content between 2 tab delimiters
3 views (last 30 days)
Show older comments
Thiago de Aquino Costa Sousa
on 2 Oct 2022
Edited: Cris LaPierre
on 3 Oct 2022
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 Comments
Accepted Answer
Cris LaPierre
on 2 Oct 2022
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 Comments
Cris LaPierre
on 3 Oct 2022
Edited: Cris LaPierre
on 3 Oct 2022
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.
More Answers (0)
See Also
Categories
Find more on Data Type Conversion 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!