MATLAB Answers

GUI How to insert multiple csv into uitable?

14 views (last 30 days)
Hello, I am new in Matlab and I am using Matlab 2019a version. I have window build in guide in Matlab, you can se how the window looks like in png file whitch I insert there. I tried many combination, some of it works but it is still not that I need and I don't know how to combine this into one which will be work. My problem is that that I have csv file which is insert there also, It includes first two rows of texts that I don't want to shows and it have first column with date and time and others columns inlude measure values which is numbers. So I want with one button to insert multiple csv into uitable without first two rows and with second button I want plot them. Now what I have. I am using uigetfile with MultiSelect on, then when I use xlsread I am able to read data but I am not able select multiple csv.
  • [num,txt,raw] = xlsread(file);
  • (assignin('base', 'data', raw));
  • set(handles.uitable1, 'Data', raw);
  • setappdata(0,'table',raw);
second what I have is this, this one is usefule for skip the first two row but can't choose multiple csv file
  • T = readtable(file,'HeaderLines',1);
  • C = table2cell(T);
  • (assignin('base', 'data', C));
  • set(handles.uitable1, 'Data', C);
  • setappdata(0,'table',C);
third is this which can be use for multiple choise
  • ds = datastore(file);
  • mydata = readall(ds);
  • C = table2cell(mydata);
  • (assignin('base', 'data', C));
  • set(handles.uitable1, 'Data', C);
  • setappdata(0,'table',C);
in the second button I have this, it is work with my date and time type
  • table = getappdata(0, 'table');
  • str = convertCharsToStrings(table(:,1));
  • DateString = datestr(str);
  • DateNumber = datenum(DateString);
  • B.Var1 = datetime(DateNumber, 'convertFrom', 'datenum','Format', 'dd.MM.yy HH:mm');
  • data = str2double(table);
  • ydata1 = data(:,2);
  • axes(handles.axes1);
  • plot(B.Var1,ydata1,'--','Color','#4DBEEE');
So it is possible to use uigetfile and choose multiple csv file with date and time and numbers and skip the first two rows and insert them into uitable and with second button plot them from uitable. Thank you very much with some idea how to deal with this. I am already lost.


Sign in to comment.

Accepted Answer

Jalaj Gambhir
Jalaj Gambhir on 7 Apr 2020
First of all, for your problem, I would recommend you to use App Designer instead of GUIDE as recommended here.
You need to break your problem into components.
  • Select multiple csv files, and read them individually.
This can be done using readTable as it provides flexibility to specify the number of header lines to be ignored, which is 2 in your case.
[filename, path] = uigetfile('*.csv','Select csv files','MultiSelect','on');
x = {}
for i = 1:length(filename)
data = readtable(fullfile(path,filename{i}), 'HeaderLines',2);
x{i} = data;
big_table = vertcat(x{:});
app.UITable.Data = big_table;
I created this in AppDesigner, button push callback function. This will help you read multiple csv files, ignore 2 header lines of each file and output the relevant data in a 'table' format. All these individual tables can then be merged into one big table, and used directly for displaying.
  • In your plotData callback, you can add your code to read the data from the UITable and plot it, using the code you have mentioned in your question.
Hope this helps!


Sarlota Duskova
Sarlota Duskova on 10 Apr 2020
Hello, thank you for your reply, I used GUIDE to App Designer Migration Tool and now when I use that code that you write, it works well with simple csv, but when i use csv file which I uploaded there, it show me this error:
Error using readtable (line 216) Reading failed at line 7. All lines of a text file must have the same number of delimiters. Line 7 has 10 delimiters, while preceding lines have 9. Note: readtable detected the following parameters: 'Delimiter', '\t ', 'MultipleDelimsAsOne', true, 'ReadVariableNames', false, 'Format', '%q%T%q%q%q%q%q%q%f%q'
Because in some row is space and somewhere not. For example I took few rows from csv file here:
  • 02-03-2020 14:25:37.477145 ;-92.3392 ;-92.3537 ;-92.3978 ;-92.2956 ;24.56 ;0;25000
  • 02-03-2020 14:25:41.475953 ; -92.348 ;-92.3802 ;-92.4368 ;-92.331 ;24.56 ;0;25000
  • 02-03-2020 14:25:45.478137 ;-92.3597 ;-92.3984 ;-92.4545 ;-92.3465 ;24.56 ;0;25000
  • 02-03-2020 14:25:49.475570 ; -92.425 ;-92.3864 ;-92.4345 ;-92.3334 ;24.56 ;0;25000
  • 02-03-2020 14:25:53.476004 ;-92.4125 ;-92.3942 ;-92.4507 ;-92.3513 ;24.56 ;0;25000
I don't know why readtable works in GUIDE with this csv file and now in App Designer has a problem. So what can I do with this random space?
Jalaj Gambhir
Jalaj Gambhir on 13 Apr 2020
Are you saying that in GUIDE, for the same csv file, readtable is working perfectly while in AppDesigner it is not? I doubt that might be the case.
Sarlota Duskova
Sarlota Duskova on 14 Apr 2020
I am apologize, it is my bad. I did this and now it is working fine:
  • data = readtable(fullfile(path,filename{i}), 'Delimiter', '\t ;', 'MultipleDelimsAsOne', true);

Sign in to comment.

More Answers (0)




Translated by