myfiles = dir(fullfile(mydir,'*.*'));
for i=1:length(myfiles);
filename = myfiles(i).name;
if myfiles.xls
emg = xlsread(filename);
elseif myfiles.csv
emg = csvread(filename);
end
I want to open both csv and xlsx extension files that are in the same folder. When I try this script it gives me an error. What is the correct script?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Oct. de 2017

0 votos

xlsread() can read csv files; you do not to handle the two differently.
But anyhow, if you have special reason for doing this, or just to see how it is done in case you want to add other formats not handled by xlsread():
myfiles = dir(fullfile(mydir,'*.*'));
for i=1:length(myfiles);
filename = myfiles(i).name;
[~, ~, ext] = fileparts(filename);
if strcmp(ext, '.xls')
emg = xlsread(filename);
elseif srcmp(ext, '.csv')
emg = csvread(filename);
end
end

5 comentarios

Debbie Oomen
Debbie Oomen el 12 de Oct. de 2017
Thank you. However in my second part of the script, matlab gives me an error that emg is an unknown variable
t = emg(:,1);
y1 = emg (:,2);
N=length(y1);
ls=size(y1);
f = 1/N; %frequency
Matlab gives the following error:Undefined function or variable 'emg'.
Walter Roberson
Walter Roberson el 12 de Oct. de 2017
That would happen for any file whose extension did not match one of the two listed. It would happen for .xlsx files for example.
myfiles = dir(fullfile(mydir,'*.*'));
for i=1:length(myfiles);
filename = myfiles(i).name;
[~, ~, ext] = fileparts(filename);
switch ext
case {'.xls', '.xslx'}
emg = xlsread(filename);
case '.csv'
emg = csvread(filename);
otherwise
fprintf('Warning: file "%s" is unrecognized extension\n', filename);
continue; %go on to next file
end
end
Debbie Oomen
Debbie Oomen el 12 de Oct. de 2017
Thank you again but still all my other codes beneath this one do not function. My workspace also just stops after this code. Warning: file "." is unrecognized extension. These are the errors I get:
Warning: file ".." is unrecognized extension Warning: file ".DS_Store" is unrecognized extension Warning: file "Vivian Static 1kg.xlsx" is unrecognized extension Warning: file "Vivian Static 2kg.xlsx" is unrecognized extension >>
Walter Roberson
Walter Roberson el 12 de Oct. de 2017
myfiles = dir(fullfile(mydir,'*.*'));
myfiles([myfiles.isdir]) = []; %skip . and .. and all other folders
for i=1:length(myfiles);
filename = myfiles(i).name;
[~, basename, ext] = fileparts(filename);
if isempty(basename)
fprintf('skipping dot file "%s"\n', filename)
continue; %go on to next file
end
switch ext
case {'.xls', '.xlsx'}
fprintf('it is xlsread for file "%s"\n', filename);
emg = xlsread(filename);
case '.csv'
fprintf('it is csvread for file "%s"\n', filename);
emg = csvread(filename);
otherwise
fprintf('Warning: file "%s" is unrecognized extension\n', filename);
continue; %go on to next file
end
fprintf('processing data with %d rows and %d columns\n', size(emg,1), size(emg,2));
t = emg(:,1);
y1 = emg (:,2);
N=length(y1);
ls=size(y1);
f = 1/N; %frequency
fprintf('processed the data in file "%s". Now what?\n', filename);
end
Debbie Oomen
Debbie Oomen el 12 de Oct. de 2017
You are a saint!! Thank you so much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fuzzy Logic Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Oct. de 2017

Comentada:

el 12 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by