Changing content between 2 tab delimiters

2 visualizaciones (últimos 30 días)
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa el 2 de Oct. de 2022
Editada: Cris LaPierre el 3 de Oct. de 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???

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 2 de Oct. de 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 = 1×22 table
A B C D E F G H I J K L M N O P Q R S T U Var22 _ _ _ _ _ __ __ __ ___ ___ __ __ ___ ___ ___ _ __ __ __ ____________ ____________ _________ 1 2 2 0 1 28 64 88 1.2 275 50 30 165 850 500 0 10 10 15 {'0,0,0,0,'} {'0,0,0,0,'} {'false'}
d.Q(1) = 11
d = 1×22 table
A B C D E F G H I J K L M N O P Q R S T U Var22 _ _ _ _ _ __ __ __ ___ ___ __ __ ___ ___ ___ _ __ __ __ ____________ ____________ _________ 1 2 2 0 1 28 64 88 1.2 275 50 30 165 850 500 0 11 10 15 {'0,0,0,0,'} {'0,0,0,0,'} {'false'}
  6 comentarios
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa el 2 de Oct. de 2022
Hi Cris... Thanks a lot... it works very well, the only problem that I still have is that, when the number that I would like to replace A.BlocIndex is two digits, after writable the new txt file only shows the first digit. Do you have any idea on how can I solve it?
if contains([data_path '/' s '/Session0' '/' parameters_new{j,1}],'Params_11','IgnoreCase',true)
A = readtable([data_path '/' s '/Session0' '/' parameters_new{j,1}]);
A.BlocIndex = 11;
writetable(A,[data_path '/' s '/Session0' '/' parameters_new{j,1}], 'Delimiter','\t');
end
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2022
Editada: Cris LaPierre el 3 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by