HELP!! Extract from multiple .csv (in table format) the column number 10

조회 수: 1 (최근 30일)
I WANT TO EXTRACT FROM MULTIPLE .CSV THE COLUMN (NUMBER 10) WITH THE NAME 'KEYRESPONSE2CORR'
I RECEIVE AN ERROR, WHY?
param='keyResponse2corr'; %Change default prameter name with required parameter
%% select folder
dataFolder = uigetdir();
filePattern = fullfile(dataFolder, '*.csv');
list = dir(filePattern);
Output = table();
for kk = 1:numel(list)
filename = list(kk).name;
data = readtable(fullfile(list(kk).folder, filename));
try
[~, varname] = fileparts(filename); % remove the file extension
Output.(varname) = data.(param);
catch ME
fprintf('Cannot find [%s] in file: %s\n %s', ...
param, filename, ME.message);
end
end
ERROR:
Cannot find [keyResponse2corr] in file: Jan_14_1131.csv
Unrecognized variable name 'keyResponse2corr'

채택된 답변

Chien-Han Su
Chien-Han Su 2020년 1월 1일
편집: Chien-Han Su 2020년 1월 1일
I assume that you already check the file 'Jan_14_1131.csv' and make sure there is a parameter 'keyResponse2corr' in the csv file.
It seems that the error message you got is directly generated from the catch block, so you may have a bug in the try block. The bug is resulted from the mismatch of number of input argument for function 'fileparts', I suggest you try to replace
[~, varname] = fileparts(filename); % remove the file extension
with
[~, varname,~] = fileparts(filename); % remove the file extension
and see if this works for you.
  댓글 수: 8
Myke Ziz
Myke Ziz 2020년 1월 1일
Thank you so much Walter!!
Very helpful!!
It worked!!
I wish you a nice evening and thank you again for all the advices!!
Walter Roberson
Walter Roberson 2020년 1월 3일
For multiple parameters:
params = {'keyResponse2corr', 'keyResponse2false'};
%% select folder
dataFolder = uigetdir();
filePattern = fullfile(dataFolder, '*.csv');
list = dir(filePattern);
Output = table();
for kk = 1:numel(list)
filename = list(kk).name;
data = readtable(fullfile(list(kk).folder, filename));
try
for P = params
thisvar = P{1};
Output.(thisvar){kk} = data.(thisvar);
end
catch ME
fprintf('Cannot find [%s] in file: %s\n %s', ...
thisvar, filename, ME.message);
end
Output.Filename{kk} = filename;
end
... If you are going to use a table() then you might as well use a table. The organization you were using was more suitable for using a struct() rather than a table()

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by