필터 지우기
필터 지우기

file reading problem with switch

조회 수: 2 (최근 30일)
Vasiliki Thomopoulou
Vasiliki Thomopoulou 2021년 1월 4일
댓글: Stephen23 2021년 1월 4일
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
Stephen23
Stephen23 2021년 1월 4일
Note that some of your switch cases include the period character, but some do not.

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 1월 4일
편집: Cris LaPierre 2021년 1월 4일
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
Vasiliki Thomopoulou
Vasiliki Thomopoulou 2021년 1월 4일
I tried but this is the error that comes up when I try to use the fileparts function.
[filepath,name,ext] = fileparts(fullpathname);
Undefined function or variable 'fExt'.
Error in fileparts (line 2)
switch lower(fExt)
Stephen23
Stephen23 2021년 1월 4일
[~,~,fExt] = fileparts(fullpathname);
% ^^^^^ use the correct variable name here

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by