How to import to Matlab many text files as table type

11 ビュー (過去 30 日間)
José Fonseca
José Fonseca 2020 年 5 月 30 日
コメント済み: Walter Roberson 2020 年 6 月 1 日
I have about 30 .txt files and I need to plot the data of each files using Matlab. I don't know how to import each of them as a table type (using the readtable function, maybe), solving this problem would help me for creating a loop that allows me to plot each imported file (I also don't have idea how to create this loop, in order to select each imported table).
  5 件のコメント
Sindar
Sindar 2020 年 5 月 31 日
Beyond the file extension, do they all store the data in the same way? Number and order of columns, data types, delimeters
José Fonseca
José Fonseca 2020 年 5 月 31 日
Sindar No, all of them are numeric values, but they can have different numer of rows and columns

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 6 月 1 日
projectdir = 'directory the files are in';
dinfo = dir(fullfile(projectdir, '*.txt')); %use appropriate extension
filenames = fullfile(projectdir, {dinfo.name});
nfiles = length(filenames);
tables = cell(nfiles,1);
for K = 1 : nfiles
tables{K} = readtable(filenames{K});
%you could plot tables{K} here
end
%or you could plot elements of tables here
for K = 1 : nfiles
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
plot(tables{K}.Time, tables{K}.Resistance, 'Displayname', basename);
hold on
end
hold off
legend show
  2 件のコメント
Sindar
Sindar 2020 年 6 月 1 日
Since the number of columns might vary, the only thing I'd add is that this will plot all later columns against the first:
plot(tables{K}{:,1}, tables{K}{:,2:end}), 'Displayname', basename);
Walter Roberson
Walter Roberson 2020 年 6 月 1 日
True. Unfortunately the legend will not be all that useful in that situation.

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

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by