Read text file, identify variables and rewrite some of the variables

5 views (last 30 days)
Hello.
I have a text file with its contents as shown below
A5Q00 0.30000000E+00 PMA: A, X, Y_i, Z 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00
0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00
A5Q01 0.30000000E+00 PMB: P, X, B_i, T 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00
0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00
A5Q02 0.30000000E+00 PMD: K, X, R_i, T 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00
0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00
i want to store these variables V1, V2, V3...V15 for each element and change their values and again write these back to a .txt file.
I have tried using split string and storing these in the cells but i have failed ot write these cells to a text file.
clc
clear all
close all
fid =fopen('Initial.txt','r')
tline = fgetl(fid);
k = 1;
while ischar(tline)
%// Store in a cell array
A{k}=strsplit(tline," ")
tline = fgetl(fid);
k = k+1;
end
V1=A{1,1}{:,1}
V2=A{1,1}{:,2}
V3=A{1,1}{:,3}
V4=A{1,1}{:,4}
V5=A{1,1}{:,5}
V6=A{1,1}{:,6}
V7=A{1,1}{:,7}

Accepted Answer

Voss
Voss on 29 Nov 2022
Edited: Voss on 29 Nov 2022
% read the file into a char vector:
fid = fopen('Initial.txt');
data = fread(fid,'*char').';
fclose(fid);
disp(data);
A5Q00 0.30000000E+00 Aqu: P, X_m_A, X_i_A, T 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00 0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00 A5Q01 0.30000000E+00 Aqu: P, X_m_A, X_i_A, T 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00 0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00 A5Q02 0.30000000E+00 Aqu: P, X_m_A, X_i_A, T 0.10000000E-16 0.10000000E-16 0.10000000E-16 0.30000000E+00 0.1045720000000E+08 0.1000000000000E-79 0.3500000000000E-01 0.2665390000000E+00
% split into a cell array, reshape:
V = reshape(strsplit(data),15,[])
V = 15×3 cell array
{'A5Q00' } {'A5Q01' } {'A5Q02' } {'0.30000000E+00' } {'0.30000000E+00' } {'0.30000000E+00' } {'Aqu:' } {'Aqu:' } {'Aqu:' } {'P,' } {'P,' } {'P,' } {'X_m_A,' } {'X_m_A,' } {'X_m_A,' } {'X_i_A,' } {'X_i_A,' } {'X_i_A,' } {'T' } {'T' } {'T' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.10000000E-16' } {'0.30000000E+00' } {'0.30000000E+00' } {'0.30000000E+00' } {'0.1045720000000E+08'} {'0.1045720000000E+08'} {'0.1045720000000E+08'} {'0.1000000000000E-79'} {'0.1000000000000E-79'} {'0.1000000000000E-79'} {'0.3500000000000E-01'} {'0.3500000000000E-01'} {'0.3500000000000E-01'} {'0.2665390000000E+00'} {'0.2665390000000E+00'} {'0.2665390000000E+00'}
% convert to numbers, where possible:
temp = str2double(V);
is_number = ~isnan(temp);
V(is_number) = num2cell(temp(is_number))
V = 15×3 cell array
{'A5Q00' } {'A5Q01' } {'A5Q02' } {[ 0.3000]} {[ 0.3000]} {[ 0.3000]} {'Aqu:' } {'Aqu:' } {'Aqu:' } {'P,' } {'P,' } {'P,' } {'X_m_A,' } {'X_m_A,' } {'X_m_A,' } {'X_i_A,' } {'X_i_A,' } {'X_i_A,' } {'T' } {'T' } {'T' } {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[ 0.3000]} {[ 0.3000]} {[ 0.3000]} {[ 10457200]} {[ 10457200]} {[ 10457200]} {[1.0000e-80]} {[1.0000e-80]} {[1.0000e-80]} {[ 0.0350]} {[ 0.0350]} {[ 0.0350]} {[ 0.2665]} {[ 0.2665]} {[ 0.2665]}
% ...
% make modifications to V, as desired
% ...
V{2,2} = 0.7 % (for example)
V = 15×3 cell array
{'A5Q00' } {'A5Q01' } {'A5Q02' } {[ 0.3000]} {[ 0.7000]} {[ 0.3000]} {'Aqu:' } {'Aqu:' } {'Aqu:' } {'P,' } {'P,' } {'P,' } {'X_m_A,' } {'X_m_A,' } {'X_m_A,' } {'X_i_A,' } {'X_i_A,' } {'X_i_A,' } {'T' } {'T' } {'T' } {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[1.0000e-17]} {[ 0.3000]} {[ 0.3000]} {[ 0.3000]} {[ 10457200]} {[ 10457200]} {[ 10457200]} {[1.0000e-80]} {[1.0000e-80]} {[1.0000e-80]} {[ 0.0350]} {[ 0.0350]} {[ 0.0350]} {[ 0.2665]} {[ 0.2665]} {[ 0.2665]}
% write a new file with the modified V:
fid = fopen('Modified.txt','w');
fprintf(fid,'%-15s %14.8E %-s %-s %10s %6s %-13s %14.8E %14.8E %14.8E %14.8E\n %19.13E %19.13E %19.13E %19.13E\n',V{:});
fclose(fid);
% view the new file's contents:
type Modified.txt
A5Q00 3.00000000E-01 Aqu: P, X_m_A, X_i_A, T 1.00000000E-17 1.00000000E-17 1.00000000E-17 3.00000000E-01 1.0457200000000E+07 1.0000000000000E-80 3.5000000000000E-02 2.6653900000000E-01 A5Q01 7.00000000E-01 Aqu: P, X_m_A, X_i_A, T 1.00000000E-17 1.00000000E-17 1.00000000E-17 3.00000000E-01 1.0457200000000E+07 1.0000000000000E-80 3.5000000000000E-02 2.6653900000000E-01 A5Q02 3.00000000E-01 Aqu: P, X_m_A, X_i_A, T 1.00000000E-17 1.00000000E-17 1.00000000E-17 3.00000000E-01 1.0457200000000E+07 1.0000000000000E-80 3.5000000000000E-02 2.6653900000000E-01
Instead of having numerous sequentially-numbered variables (V1, V2, V3, etc.) it's better to have a single variable that you can index (V(1), V(2), V(3), ..., or V{1}, V{2}, V{3}, ..., or V(1,:), V(2,:), V(3,:), ..., etc.)
  6 Comments
shadman khan
shadman khan on 6 Dec 2022
Edited: shadman khan on 8 Dec 2022
Dear @Voss
here's th link o the new question. I hope you can take a look when you are available: https://in.mathworks.com/matlabcentral/answers/1871827-read-text-file-identify-variables-combine-some-the-variables-and-write-these-variables-to-another

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 29 Nov 2022
I'd probably read this into a table array. A first pass at reading in this file is below. You probably want to customize the import options (perhaps using the Import Tool, uiimport, to allow you to interactively explore the effect of different options.)
thefile = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1212458/Initial.txt';
The file seems to be space delimited, but let's treat multiple spaces as one delimiter.
importOptions = detectImportOptions(thefile, 'Delimiter', ' ', ...
'ConsecutiveDelimitersRule', 'join');
By default readtable takes the first row as the list of variable names, but the first row of your file is data. So let's specify some default names and say to read all the lines as data.
importOptions.VariableNames = "Var" + (1:numel(importOptions.VariableNames));
importOptions.DataLines = [1 Inf];
Now actually import the data.
T = readtable(thefile, importOptions)
T = 6×11 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 __________ __________ _____ _____ _______ __________ __________ _____ _____ _____ _____ {'A5Q00' } 0.3 NaN NaN NaN {'X_i_A,'} {'T' } 1e-17 1e-17 1e-17 0.3 {0×0 char} 1.0457e+07 1e-80 0.035 0.26654 {0×0 char} {0×0 char} NaN NaN NaN NaN {'A5Q01' } 0.3 NaN NaN NaN {'X_i_A,'} {'T' } 1e-17 1e-17 1e-17 0.3 {0×0 char} 1.0457e+07 1e-80 0.035 0.26654 {0×0 char} {0×0 char} NaN NaN NaN NaN {'A5Q02' } 0.3 NaN NaN NaN {'X_i_A,'} {'T' } 1e-17 1e-17 1e-17 0.3 {0×0 char} 1.0457e+07 1e-80 0.035 0.26654 {0×0 char} {0×0 char} NaN NaN NaN NaN
  1 Comment
shadman khan
shadman khan on 29 Nov 2022
Hi Steven thanks for your reply. I like the compact code, but its not providing the few variables like Var3, Var4, etc.,

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!