Read text file, identify variables and rewrite some of the variables
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2022-11-29
编辑:Voss
2022-11-29
% 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 个评论
Voss
2022-12-4
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.
更多回答(1 个)
Steven Lord
2022-11-29
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
