필터 지우기
필터 지우기

what function should I use to download a text file (.txt) ?

조회 수: 6 (최근 30일)
Yelena Kokotova
Yelena Kokotova 2023년 11월 16일
댓글: Yelena Kokotova 2023년 11월 20일
I found this code on the Internet
clear all
sf='D:\Iglin\Matlab\ContData\xrayl.txt';
x=load(sf);
x=sort(x(:));
n=length(x);
xmin=x(1);
xmax=x(n);
fprintf('Обрабатываем файл %s\n',sf)
fprintf('Объем выборки n=%d\n',n)
fprintf('xmin=%14.7f\n',xmin)
fprintf('xmax=%14.7f\n',xmax)
it gives an error
Error using load
Unable to find file or directory 'D:\Examples\xrayl.txt'.
,
I read that the function load is not suitable for extension files .txt
I found another function on the internet: importdata
clear all % очистить память
sf='D:\Examples\xrayl.txt'; % имя файла данных
x=importdata(sf); % вводим ИД
x=sort(x(:)); % переформатировали столбец и рассортировали
n=length(x); % количество данных
xmin=x(1); % минимальное значение
xmax=x(n); % максимальное значение
fprintf('Обрабатываем файл %s\n',sf)
fprintf('Объем выборки n=%d\n',n)
fprintf('xmin=%14.7f\n',xmin)
fprintf('xmax=%14.7f\n',xmax)
matkad writes
Error using importdata
Unable to open file.
can you tell me if there are any other functions?
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 11월 16일
Generally speaking, it is possible to load() some txt files:
  • lines that start with % in the file are ignored
  • all other lines must only have numbers and delimiters
  • must be the same number of numeric entries on each line
  • comma delimiter, space delimiter, tab delimiter are all acceptable
So simple blocks of numbers can work with load.
But these days, readmatrix or readtable are generally better as they are more flexible and have better error control.
Yelena Kokotova
Yelena Kokotova 2023년 11월 20일
Thanks very much.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 11월 16일
It really depends what is in the *.txt file and how the data formatted (numerical, text or mix, columnwise, rowise, etc.). See these three examples of *.txt files on how to import or read their data:
FN1 = 'DATA0.txt';
% ALL succeed: load, readmatrix, readtable, importdata, textread. NOTE
% textread() is not recommended --> textscan recommended.
D01 = load(FN1)
D01 = 2×11
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 0 0.7513 0.8841 0.9356 0.5431 0.1311 -0.3876 -0.6483 -0.8622 -0.5626 0.0013
D02 = readmatrix(FN1)
D02 = 2×11
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 0 0.7513 0.8841 0.9356 0.5431 0.1311 -0.3876 -0.6483 -0.8622 -0.5626 0.0013
D03 = readtable(FN1)
D03 = 2×11 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 ____ ______ ______ ______ ______ ______ _______ _______ _______ _______ ______ 0 1 2 3 4 5 6 7 8 9 10 0 0.7513 0.8841 0.9356 0.5431 0.1311 -0.3876 -0.6483 -0.8622 -0.5626 0.0013
D04 = importdata(FN1)
D04 = 2×11
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 0 0.7513 0.8841 0.9356 0.5431 0.1311 -0.3876 -0.6483 -0.8622 -0.5626 0.0013
D05 = textread(FN1)
D05 = 2×11
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 0 0.7513 0.8841 0.9356 0.5431 0.1311 -0.3876 -0.6483 -0.8622 -0.5626 0.0013
% ALL succeed BUT importdata reads differently than load, readmatrix,
% readtable, textread
FN2 = 'MN2.txt';
D01 = load(FN2)
D01 = 10×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 19 26 3 10 17 25 7 9 16 18 6 8 15 22 24 12 14 21 23 5 13 20 27 4 11
D02 = readmatrix(FN2)
D02 = 10×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 19 26 3 10 17 25 7 9 16 18 6 8 15 22 24 12 14 21 23 5 13 20 27 4 11
D03 = readtable(FN2)
D03 = 10×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 19 26 3 10 17 25 7 9 16 18 6 8 15 22 24 12 14 21 23 5 13 20 27 4 11
D04 = importdata(FN2)
D04 = 50×1
17 24 1 8 15 23 5 7 14 16
D05 = textread(FN2) % Not recommended
D05 = 10×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 19 26 3 10 17 25 7 9 16 18 6 8 15 22 24 12 14 21 23 5 13 20 27 4 11
% ALL except for textread reads/imports but quite differently and insufficiently well. In this
% case, readtable and importdata do the job very well
% See how textscan works well and reads all data correctly
FN3 = 'DATE_Data.txt';
D01 = load(FN3)
D01 = 5×3
1.0e+03 * 2.0160 0 0.0130 2.0220 0 0.0155 2.0230 0 0.0232 2.0160 0 0.0232 2.0210 0 0.0111
D02 = readmatrix(FN3)
D02 = 5×3
NaN NaN 13.0000 NaN NaN 15.5000 NaN NaN 23.2500 NaN NaN 23.2300 NaN NaN 11.1300
D03 = readtable(FN3)
D03 = 5×3 table
Var1 Var2 Var3 ___________ ________ _____ 18-Feb-2016 00:00:00 13 18-Feb-2022 00:00:00 15.5 18-Feb-2023 00:00:00 23.25 18-Feb-2016 00:00:00 23.23 18-Feb-2021 00:00:00 11.13
% readtable with options: D03 and DO3B are the same.
OPTIONs = detectImportOptions(FN3);
D03B = readtable(FN3, OPTIONs)
D03B = 5×3 table
Var1 Var2 Var3 ___________ ________ _____ 18-Feb-2016 00:00:00 13 18-Feb-2022 00:00:00 15.5 18-Feb-2023 00:00:00 23.25 18-Feb-2016 00:00:00 23.23 18-Feb-2021 00:00:00 11.13
D05 = importdata(FN3)
D05 = 5×1 cell array
{'2016-02-18 00:00:00 13.0' } {'2022-02-18 00:00:00 15.5' } {'2023-02-18 00:00:00 23.25'} {'2016-02-18 00:00:00 23.23'} {'2021-02-18 00:00:00 11.13'}
fileID = fopen(FN3);
Format_OPTIONs = '%s %s %f'; % Note the format specs
D06 = textscan(fileID, Format_OPTIONs)
D06 = 1×3 cell array
{5×1 cell} {5×1 cell} {5×1 double}
fclose(fileID)
ans = 0
D06{1}
ans = 5×1 cell array
{'2016-02-18'} {'2022-02-18'} {'2023-02-18'} {'2016-02-18'} {'2021-02-18'}
D06{3}
ans = 5×1
13.0000 15.5000 23.2500 23.2300 11.1300
D07 = textread(FN3) % Fails without specifying the data format in FN3
Error using dataread
Trouble reading number from file (row 2, field 2) ==>

Error in textread (line 124)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by