How to open csv file?
조회 수: 164 (최근 30일)
이전 댓글 표시
I am needing to loop through a whole folder of files, but I am struggling to figure out how to read/open the csv files once I have gotten this far:
% This counts the number of files that will need to be looped through, so
% pick the folder with all of the data desired to be run
source_dir = uigetdir([]);
d = dir([source_dir, '\*.csv']);
z = length(d);
%Get information about what's inside your folder.
myfiles = dir(source_dir);
%Get the filenames and folders of all files and folders inside the folder
%of your choice.
filenames = {myfiles(:).name}';
filefolders = {myfiles(:).folder}';
%Get only those files that have a csv extension and their corresponding
%folders.
csvfiles = filenames(endsWith(filenames,'.csv'));
csvfolders = filefolders(endsWith(filenames,'.csv'));
%Make a cell array of strings containing the full file locations of the
%files.
files = fullfile(csvfolders,csvfiles);
Filenames = string(filenames);
Filefolders = string(filefolders);
댓글 수: 0
채택된 답변
추가 답변 (1개)
Cris LaPierre
2020년 9월 16일
There are many ways to import a csv file, including
- readtable
- textscan
- readmatrix
- csvread (not recommended)
- dlmread (not recommended)
- read in combination with a datastore (good approach if there are a lot of files. See this video for an example)
What is best depends on the contents of your files.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Standard File Formats에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!