what function should I use to download a text file (.txt) ?
2 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 16 Nov 2023
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.
Answers (1)
Sulaymon Eshkabilov
on 16 Nov 2023
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)
D02 = readmatrix(FN1)
D03 = readtable(FN1)
D04 = importdata(FN1)
D05 = textread(FN1)
% ALL succeed BUT importdata reads differently than load, readmatrix,
% readtable, textread
FN2 = 'MN2.txt';
D01 = load(FN2)
D02 = readmatrix(FN2)
D03 = readtable(FN2)
D04 = importdata(FN2)
D05 = textread(FN2) % Not recommended
% 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)
D02 = readmatrix(FN3)
D03 = readtable(FN3)
% readtable with options: D03 and DO3B are the same.
OPTIONs = detectImportOptions(FN3);
D03B = readtable(FN3, OPTIONs)
D05 = importdata(FN3)
fileID = fopen(FN3);
Format_OPTIONs = '%s %s %f'; % Note the format specs
D06 = textscan(fileID, Format_OPTIONs)
fclose(fileID)
D06{1}
D06{3}
D07 = textread(FN3) % Fails without specifying the data format in FN3
See Also
Categories
Find more on Manage Products 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!