Read multiple txt files and plot all in one graph

8 views (last 30 days)
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.
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

Answers (1)

Sourabh Kondapaka
Sourabh Kondapaka on 7 Aug 2020
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
  1 Comment
Phi Kevin Ho
Phi Kevin Ho on 10 Aug 2020
Thank you very much! The code works good now. I had to add properties(Access = private) to enter file, path columnName. x and y because the properties (Access = public) isn't editable.
I figuered out two more issues...
I tried to add a legend so I can see which line belongs to which data. My idea was to add the line
legend(app.UIAxes, t.Properties.VariableNames{2})
It works fine for the first file but by adding another file it doesn't add another filename. Instead it shows something different.
The only function that is working by keeping updating the legend is
legend(app.UIAxes, app.UITable.ColumnName)
But the Problem is that the first line is being named by the first column and the second line by the second column.
The second issue is, that I get a problem if the other files don't have the same amount of rows like the first one. Do you have any advice so I can try to fix that problem?

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!