how to read from disk a text file containing columns of numbers and columns of characters
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jack Cohen
el 11 de Mayo de 2020
Respondida: Guru Mohanty
el 14 de Mayo de 2020
I have on disk a .txt file, containing
a column of dates (23/2/20) and, two columns of integers.
As an example, the first 3 lines look like that:
23/2/20 190 11000
24/2/20 310 90
25/2/20 15 14000
How to read this file from disk ?
Thanks !
Jack
2 comentarios
Respuesta aceptada
Guru Mohanty
el 14 de Mayo de 2020
Hi, I understand you are trying to read data from a txt file and store processed data in another txt file. You can do this using the following functions.
- importdata - To access data from a txt file
- fopen - To create a new file
- fprintf - To Write data in the text file.
Here is a sample code for it.
clc;clear all;
% File Read
filename = 'test.txt';
A = importdata(filename);
Dates=A.textdata;
Column1=A.data(:, 1);
Column2=A.data(:, 1);
% File Write
fid = fopen( 'results.txt', 'wt' );
for i = 1:length(A.textdata)
fprintf( fid, '%s %d %d\n', Dates{i}, Column1(i), Column2(i));
end
fclose(fid);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!