file reading problem with switch
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey guys, I am posting this here hopping someone can help. I run the code and the thing is that even though I select an csv file the function doesnt read it, and it goes straight to displaying the error message. Does anyone have an idea why this happens? Thanks a lot in advance!!
function a=diavaseto
[fn, pn] = uigetfile({'*.csv'; '*.ods'; '*.xlsx'}, 'Epilogh arxeioy dedomenwn');
fullpathname=fullfile(pn, fn);
extIndices= strfind('.',fn);
fext = fn(max(extIndices):length(fn));
switch lower(fext)
case '.csv'
a=readtable(fullpathname);
% A csv file
case '.ods'
a=xlsread(fullpathname);
% An ods file
case 'xlsx'
a=xlsread(fullpathname);
%An xlsx file
otherwise
error('Unexpected file extension: %s', fext);
end
1 Kommentar
Stephen23
am 4 Jan. 2021
Note that some of your switch cases include the period character, but some do not.
Antworten (1)
Cris LaPierre
am 4 Jan. 2021
Bearbeitet: Cris LaPierre
am 4 Jan. 2021
My suggestion is to use fileparts rather than trying to come up with your own way to extract the file extension.
If you want to use strfind, make sure you put the inputs in the correct order. You have them reversed. It should be
extIndices= strfind(fn,'.');
2 Kommentare
Stephen23
am 4 Jan. 2021
[~,~,fExt] = fileparts(fullpathname);
% ^^^^^ use the correct variable name here
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!