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

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 日

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 日
Hi thank you a lot for your fast answer!!
Unfortunately I still get an error.
I add a picture of the table Matlab is reading.
Schermata 2020-01-01 alle 18.37.03.png
Cannot find [keyResponse2_corr] in file: ....
Walter Roberson
Walter Roberson 2020 年 1 月 1 日
When the error occurs, what shows up for
data.Properties.VariableNames
Myke Ziz
Myke Ziz 2020 年 1 月 1 日
Hi Robert,
it shows as if they were cell arrays, should I change the table format?
Schermata 2020-01-01 alle 19.27.04.png
Walter Roberson
Walter Roberson 2020 年 1 月 1 日
You have
Output.(varname) = data.(param);
That can also fail if varname is not a valid table variable name. In particular, until R2019b, the names had to be valid MATLAB variable names; it was not until R2019b that they added the ability to use arbitrary character vectors as table variable names.
Myke Ziz
Myke Ziz 2020 年 1 月 1 日
編集済み: Myke Ziz 2020 年 1 月 1 日
How should I correct the error then?
Use another Matlab version?
Thank you a lot for your help!!
I am currently using the matlab 2018b
Walter Roberson
Walter Roberson 2020 年 1 月 1 日
After your line
[~, varname] = fileparts(filename); % remove the file extension
(which is fine by itself, you do not need the extra ~ output) add
varname = genvarname(varname, fieldnames(Output));
This will modify the file name into something that is a valid variable name and that is different from all of the other names generated so far.
However, the result might not be all that readable. For example in a test I did just now, some of the generated names included
{'ishg2' }
{'jbigkit0x2D20x2E10x2Etar' }
{'jgdsens0x2Dpa0x2Dwavplay0x2D46c387a' }
{'joe0x2Dof0x2Dall0x2Dtrades0x2Dxml2struct0x2D91c3b8a' }
{'johnyf0x2Dfig2u3d0x2Dv10x2E00x2E10x2D20x2Dg74fe75d' }
{'jpeg_toolbox' }
Field names such as 'johnyf0x2Dfig2u3d0x2Dv10x2E00x2E10x2D20x2Dg74fe75d' (which was originally 'johnyf-fig2u3d-v1.0.1-2-g74fe75d') are essentially unreadable messes to humans, and the uniqueness requirements imply that you cannot predict the new name given just the original name alone. You should be reconsidering using the file names as field names for your struct. Consider using a cell array in which one column is the table and the other column is the file name.
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 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by