Error using readtable '.1(0a)' is not a recognized file extension. Unable to detect file type.

37 次查看(过去 30 天)
This is my script:
%{
Load data for exp 2.1
load all files
Calculate the quantitative EMG value for each data set
Generate mothods figure with raw EMG. integrated EMG, quant EMG
Generate a results figure with the quant EMMG before and after fatigue
%}
%%
clear all
% extract files that have '2.1' in them
files = dir("*2.1*");
S = struct;
figure(1);clf
t1 = tiledlayout('flow');
for a = 1:length(files)
T_name = ['Exp_'+ string(files(a).name)]; % creates dynamic name
S.(T_name) = readtable(files(a).name);
nexttile
plot(S.(T_name).Time_s_.S.(T_name).AI0HPF_V_)
title(strrep(T_name,'_',' '));
clear T_name
end
t1.Padding = 'compact';
t1.TileSpacing = 'compact';
And this is the error message I'm recieving:
Error using readtable
'.1(0a)' is not a recognized file extension.
Unable to detect file type. To read the file
as a specific file type, regardless of file
extension, use the 'FileType' name-value
pair.
Error in tutorialplots (line 26)
S.(T_name) = readtable(files(a).name);
My files are named "2.1(0a)" and so on and I guess I'm unsure of what the filetype actually is. The contents of the files look like .csv files. The column headers read "Time (s), AI0 HPF (V), AI0 MWSDF (V), AI1 HPF (V), AI1 MWSDF (V)".

采纳的回答

Stephen23
Stephen23 2023-10-23
I strongly recommend avoiding forcing meta-data (e.g. filenames) into structure fields like that. To make your approach work would require mangling the filenames using e.g. matlab.lang.makeValidName and there is no guarantee that the names would be unique after that, so that approach is fundamentally fragile and could lead to loss of data without any warning. Best avoided.
Here is a simpler, much more robust approach which does not create any superfluous data arrays (e.g. extra cell arrays) by simply using the structure array that you already have returned from DIR:
S = dir("*2.1*");
for k = 1:numel(S)
% import
F = fullfile(S(k).folder,S(k).name)
T = readtable(F);
S(k).data = T;
% plot
nexttile
plot(T.Time_s_, T.AI0HPF_V_)
..
end
This is simpler and easier to access using very basic, efficient indexing. For example the 2nd file:
S(2).name % filename
S(2).data % imported file data

更多回答(1 个)

the cyclist
the cyclist 2023-10-23
MATLAB can't autodetect the file type, and you're not sure either, so you can try to experiment. You can specify a ('Name','Value') argument pair, as mentioned in the documentation this section of the readtable documentation.
For example, you could try
readtable(files(a).name,'FileType','text')
since it seems CSV-ish.
There are more sophisticated ways to specify the file layout (e.g. textscan), but I would try a few file types first, and see if that works.
  4 个评论
hyu34gyd5
hyu34gyd5 2023-10-23
I do want the content of the file to be stored into S.Exp_2.1(0a), but the code was previously used for file names such as '21_0_1' where 21 is the experiment (2.1), 0 is the weight used, and 1 is the trial number (a).
Walter Roberson
Walter Roberson 2023-10-23
and you should makeing
filenames = {files.name};
to record the file names; and saving into a cell array of tables:
S = cell(length(filenames), 1);
for a = 1 : length(filenames);
S{a} = readtable(filenames{a});
end
This separates data from variable names.
You could put them together afterwards into a struct if you wanted,
SS = struct('filename', filenames(:), 'Data', S);
This would create a struct array with fields 'filename' and 'Data' if you had a particular reason to want to code as a struct array instead of as a pair of variables filenames and S

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by