How to process multiple .csv files?

조회 수: 3 (최근 30일)
flemingtb
flemingtb 2018년 11월 2일
답변: flemingtb 2018년 11월 2일
I have a folder of .csv files i'd like to process as a batch, meaning perform analysis on each file individually. Here is the code to open the files, that i found. I'm getting an error i don't understand.
myFolder = 'B:\Projects\Concert\OfflineWarpGauge\FF_Data\Day1';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
end
Error Message:
Function 'subsindex' is not defined for values of class 'struct'.
Error in BatchProcessFiles (line 13)
for k = 1 : length(theFiles)

채택된 답변

Stephen23
Stephen23 2018년 11월 2일
편집: Stephen23 2018년 11월 2일
You have a variable named length. This means when you write this:
for k = 1 : length(theFiles)
you are telling MATLAB that you want to indexing into the variable length using theFiles (which is a structure). Structures are not able to be used as indices, so this throws an error.
Solution: rename that variable, and call this:
clear length

추가 답변 (3개)

madhan ravi
madhan ravi 2018년 11월 2일

flemingtb
flemingtb 2018년 11월 2일
Wow that worked. Basically it's opening each file and doing the analysis that i normally was doing one at a time. The data is a matrix of (Z) height data, where all i am doing is calculating the range and creating a contour plot for each.
The batch file code is creating the contour plots but the only range i'm getting is the range of the last file. How can i get a range value for each file?
  댓글 수: 1
Stephen23
Stephen23 2018년 11월 2일
편집: Stephen23 2018년 11월 2일
@flemingtb: see the link that madhan ravi gave: this shows how to store imported file data in a cell array. Then you can access all of that imported data using cell indexing or a comma-separated list, e.g.:
C = cell(...);
for k = ...
C{k} = ...
end
M = horzcat(C{:})

댓글을 달려면 로그인하십시오.


flemingtb
flemingtb 2018년 11월 2일
Got it, thanks.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by