how to read from disk a text file containing columns of numbers and columns of characters

2 ビュー (過去 30 日間)
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 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 5 月 11 日
Jack - have you tried using importdata?
Jack Cohen
Jack Cohen 2020 年 5 月 12 日
Not until now, but I can see I''ll do it ASAP !
Meanwhile, let me first to warmly thank you,for you help !
And also, could you solve for me the inverse problem:
Supposing I created in my editor a table file, containing 1 column of dates (12/05/2020 etc) and
3 columns of integers, how could I save this , as a disk file which can subsequently be printed ?
Thanks for your time and Best Regards,
Jack

サインインしてコメントする。

採用された回答

Guru Mohanty
Guru Mohanty 2020 年 5 月 14 日
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.
  1. importdata - To access data from a txt file
  2. fopen - To create a new file
  3. 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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by