How to make a CSV parser for multiple files with different names?

5 views (last 30 days)
I am trying to make a parser of multiple CSV files here. Here's a solution for one. This allows me to pick one of the files and work with it. All I want to do with each file is to find the maximum value with respect to other columns. For example, here's the example of output I get:
function upload_btn_Callback(~, ~, handles)
%Uploading a file via user interface
uploaded_file = uigetfile('*.csv','Select a .csv data file');
%If file is loaded
if uploaded_file == 0
set(handles.upload_txt, 'String', 'Status: Cancelled');
else
set(handles.upload_txt, 'String', 'Status: Please wait...');
%Loading the array with CSV-file values
array = csvread(uploaded_file, 8, 0);
set(handles.upload_txt, 'String', ['Uploaded: ' uploaded_file]);
format long;
%Finding all max velocity values
max_vel = max(array(:, 5));
idx = find(array(:, 5) == max_vel);
%Finding time of the max velocity values
t = array(idx(:, 1), 3);
time = num2str(t);
%Finding altitude of the max velocity values
a = array(idx(:,1), 4);
alt = num2str(a);
set(handles.uitable1, 'Visible', 'on');
set(handles.pushbutton2, 'Visible', 'on');
set(handles.uitable1, 'Data', {num2str(max_vel), alt, time});
%This doesn't work...
plot(array(:, 3), array(:, 5));
end
With all this I can't figure out how to make it work for multiple files. Ultimately all I need to do is to pick a directory with those files and load all files with .csv extension, no matter what their names are. I tried different ways with dir() and other stuff but none would work... Maybe there's a better way to do, other than this? I honestly wouldn't even do it in MATLAB if it weren't for its speed with matrices. (Files are hundreds of megabytes big).
Thank you!

Answers (2)

Walter Roberson
Walter Roberson on 30 Nov 2015
  1 Comment
Walter Roberson
Walter Roberson on 30 Nov 2015
folder = uigetdir('What folder do you want?');
dirinfo = dir(fullfile(folder, '*.csv'));
for K = 1 : length(dirinfo)
uploaded_file = fullfile(folder, dirinfo(K).name);
array = csvread(uploaded_file, 8, 0);
... etc etc...
end

Sign in to comment.


Micard
Micard on 30 Nov 2015
I have seen that, and this does not help me in my situation. I need to use User Interface to locate a specific folder and use ALL the files in that directory (i.e. I don't want the user to pick all the files manually). In addition, the process described in that example cannot be merged (implemented) with my code, at least not the way I tried.

Categories

Find more on Interactive Control and Callbacks 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!