Changing content between 2 tab delimiters

조회 수: 2 (최근 30일)
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022년 10월 2일
편집: Cris LaPierre 2022년 10월 3일
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???

채택된 답변

Cris LaPierre
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 = 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
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022년 10월 2일
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 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by