How to select and read csv data file using uigetfile?

38 vues (au cours des 30 derniers jours)
Arshey Dhangekar
Arshey Dhangekar le 5 Oct 2021
Commenté : Mathieu NOE le 11 Oct 2021
I want to implement select and read file using uigetfile. How can I implment in my code to read the data?
clc
clear all
[file,path,~]=uigetfile('*.csv');
T=fullfile(path,file);
T = readtable(T);
T.Time=minutes(T.Time)
  1 commentaire
Jan
Jan le 6 Oct 2021
warning off is an extremely bad idea. Warnings are essential und useful.
Your code contains an import of the files already. So what exactly is your question?

Connectez-vous pour commenter.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 6 Oct 2021
Modifié(e) : Mathieu NOE le 6 Oct 2021
hello
if you want to grad multiple files with uigetfile, you can do that :
% It is important to grab the files in ascending order
file_list = uigetfile('*.csv', 'Grab the files you want to process', 'MultiSelect', 'on');
if iscell(file_list) == 0
file_list = {file_list};
end
but I find more efficient to use this , in order to load automatically all files once sorted properly (what matlab is not good at)
how to use it : (example)
%% read multiple files
P = pwd; % currrent directory
S = dir(fullfile(P,'*.csv')); % get list of files in directory
fileNames_sorted = natsortfiles({S.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
nFiles = numel(fileNames_sorted);
for k = 1:nFiles
F = fullfile(P, fileNames_sorted{k});
S(k).data = csvimport(F); % or READTABLE or whatever.
end
% Take a look in the structure S: it contains all of your file data and the corresponding filenames, just as you require.
% For example, the 2nd filename and its data:
S(2).name
S(2).data
% Accessing and processing this will be much simpler than messing around with dynamically named variables.

Plus de réponses (0)

Catégories

En savoir plus sur Data Acquisition Toolbox Supported Hardware dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by