- The reading section will give you 'text_cell' as a cell array that contains all text data, each text line as a cell of the cell array.
- you can edit or update any cell value as like in section updating data.
- you can rewrite an updated file as like section writing updated text file.
- you can find your expected header text position by comparing '*' with each cell array text by using function strfind.
how to read text file and write same text file ?
44 views (last 30 days)
Show older comments
pruth
on 15 Oct 2019
Answered: Neeraj Kaberpanthi
on 15 Oct 2019
Hello matlab community. I have this text file with me which i need to read. I have some experince to read structured data from text file but this file is very new to me.
can somebody of you help me to read this file ? and more thing i need to do here is i need to write the same file after some processing. so my 2nd question is how to write the same text file with same headers and values we alredy read !
(ps: the values may change after processing but as for now lets say i need to write same values)
* upper layer boundaries above ground in km
30.0
20.0
15.0
10.0
9.0
8.0
7.0
6.5
6.0
5.5
5.0
4.5
4.0
3.8
3.6
3.4
3.2
3.0
2.8
2.6
2.4
2.2
2.0
1.8
1.6
1.4
1.2
1.0
0.8
0.6
0.4
0.2
* number of layers
32
20
* Sa read in?
False
* scaling parameter for Sa matrix
0.5
* real elevation angle for zenith direction
0.001
* Sa correlation length in km
0.2
* input filename
"work0_O4//QDOAS.asc"
* surface albedo
0.06
* output directory path
"work0_O4/"
* temperature pressure filename
"work0_O4/temppress_data.txt"
* wavelength in nm
360.7731
* altitude in km
0.01
* cross section folder
"/home/iitm/Desktop/MMF_vlidort/TEST2/CONST/O4_298.xs"
* heights for apriori profile filename
"/home/iitm/Desktop/MMF_vlidort/TEST2/STINPUTS/apriori_heights.txt"
* vlidort config filename
"/home/iitm/Desktop/MMF_vlidort/TEST2/CONST/2p6_VLIDORT_ReadInput.cfg"
* aerosol ext t, unisotropy g, omega w
0.414
0.68
0.92
* apriori profile filename
"/home/iitm/Desktop/MMF_vlidort/TEST2/STINPUTS/aerosol_apriori.txt"
every sentence coming after * is a header.
0 Comments
Accepted Answer
Neeraj Kaberpanthi
on 15 Oct 2019
Hi,
You should try this code.
hope it will help you.
clc
clear
%% Reading text file
file_name='setup_generic.txt';
file_id=fopen(file_name,'r');
text_cell=cell(1);
while 1
text_line_read=fgetl(file_id);
if text_line_read == -1
break
else
text_cell(end,1)=cellstr(text_line_read);
text_cell(end+1,1)=cell(1);
end
end
fclose(file_id);
%% updating data
% lets change cell array index 2 which is 30.0 to 34.0
text_cell{2,1}='34.0';
%% write updated text file
file_id=fopen(file_name,'w');
for i=1:length(text_cell)
fprintf(file_id,'%s\n', text_cell{i});
end
fclose(file_id);
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Export 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!