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???

 Réponse acceptée

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 commentaires

Hi Cris, thank you for replying me. Yes, I will need to get the table back into a text file. But the function writetable is showing error Error using writetable Too many output arguments. I don't know if it is because of the first lines of my files that is :
Data file generated on Tue Aug 30 12:40:29 2022
and also some lines in blank... Do you have any suggestion?
Cris LaPierre
Cris LaPierre le 2 Oct 2022
Modifié(e) : Cris LaPierre le 2 Oct 2022
You haven't shared your code, but it sounds like you have too many output arguments (variables to the left of the equals sign). With writetable, you can't have any variables to the left. The correct syntax is
Try this
writetable(d,'myfile2.txt','Delimiter','\t')
I was trying with this code, where I loaded the file through readtable, then I made the correction base on a logical, and then I need to return it for a txt file, rewriting the original file...
A = readtable([data_path '/' s '/Session0' '/' parameters_new{j,1}]);
if contains([data_path '/' s '/Session0' '/' parameters_new{j,1}],'Params_10','IgnoreCase',true)
A.BlocIndex = 10;
A = writetable(A, 'Delimiter','\t');
fid = fopen([data_path '/' s '/Session0' '/' parameters_new{j,1}], 'w');
fprintf(fid, '%s\n', A{:});
fclose(fid);
end
writetable writes the table to file. If you don't specify the filename, it uses the table name. I don't understand what you are doing with fopen and fprintf. I'd remove it, as it seems unnecessary to me.
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 le 3 Oct 2022
Modifié(e) : Cris LaPierre le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by