Read text file, identify variables and rewrite some of the variables
1 view (last 30 days)
Show older comments
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}
0 Comments
Accepted Answer
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);
% split into a cell array, reshape:
V = reshape(strsplit(data),15,[])
% convert to numbers, where possible:
temp = str2double(V);
is_number = ~isnan(temp);
V(is_number) = num2cell(temp(is_number))
% ...
% make modifications to V, as desired
% ...
V{2,2} = 0.7 % (for example)
% 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
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
Voss
on 4 Dec 2022
Hi @shadman khan, I am unable to look into this in detail at the moment, so you may want to open a new question about this issue with the space in the parameter name, and link back to this question/thread.
More Answers (1)
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)
See Also
Categories
Find more on String Parsing 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!