Read multiple txt files and plot all in one graph
6 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm using the app designer for my project. My goal is to read multiple txt files and plot them all in one single graph so I can see if the data is similar or not.
I figured out how to do it with one file. But I'm struggeling with more files. The code I'm using for one file is listed down below (callback function of the "Read Data" button. In the uploaded picture you can see what I was trying to do.
I would like to press "Read Data" and choose multiple files, read them in so they will be shown in Column 1, Column 2, Column n... and one graph where all data will be shown.
While browsing through older threads, I found this link here: https://de.mathworks.com/matlabcentral/answers/443167-importing-multiple-txt-files-into-one-table
The problem I have is that I somehow can't figure out how to transfer it to my project especially because I don't need the delimited function and the fact that I have two columns.
I'd be really happy if someone can help or guide me through my problem.
function ReadDataButtonPushed(app, event)
t ) readtable(uigetfile('*.txt'), 'Format', '%f %f');
app.UITable.Data= t;
t.Properties.VariableNames{1} = 'Time_s'; %Just in case I want to change the names
t.Properties.VariableNames{2} = 'AI_V';
app.UITable.ColumnName = t.Properties.VariableNames;
x = table2array(t(:, "Time_s"));
y = table2array(t(:, "AI_V"));
plot(app.UIAxes,x,y);
end
0 个评论
回答(1 个)
Sourabh Kondapaka
2020-8-7
Hi,
I have updated your callback function. When the first file is selected, as the “Data” property of “app.UITable” will be empty it will fill out both the columns.
For all subsequent files that are selected, they get appended to the “app.UITable” and the “app.UIAxes” is updated.
I had changed naming the "AI_V" column to be more specific from the file it has been imported from.
The names of columns that will be generated for "AI_V" column would be "AI_V_Sample1-Test1", "AI_V_Sample2-Test2" and "AI_V_Sample3-Test3".
% These are the properties that will be used in the "ReadDataPushed()" callback function.
properties (Access = public)
file; % File Name.
path; % File Path.
columnName; % Name of the column that will be specific to the imported file.
x; % x-axis data to plot.
y; % y-axis data to plot.
end
function ReadDataPushed(app, event)
[app.file, app.path] = uigetfile('*.txt');
t = readtable(fullfile(app.path, app.file), 'Format', '%f %f');
if isempty(app.UITable.Data)
app.UITable.Data = t;
t.Properties.VariableNames{1} = 'Time_s';
t.Properties.VariableNames{2} = ['AI_V' '_' app.file(1:end -4)];
app.UITable.ColumnName = t.Properties.VariableNames;
else
app.columnName = ['AI_V' '_' app.file(1:end-4)];
app.UITable.Data.(app.columnName) = t{:,2};
app.UITable.ColumnName = [app.UITable.ColumnName; app.columnName];
end
app.x = app.UITable.Data{:, 1};
app.y = app.UITable.Data{:,2:end};
plot(app.UIAxes, app.x, app.y);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!