필터 지우기
필터 지우기

Reading mat or ascii files

조회 수: 7 (최근 30일)
Jonasz
Jonasz 2013년 10월 7일
편집: dpb 2013년 10월 7일
Hello. I have a strange problem. I am reading two types of files '-ascii' and '-mat' but the load function must be set to one of them but I don't know what tpye of file is currently reading. My question is how to write a code so it will be work for both types of files.

채택된 답변

Jan
Jan 2013년 10월 7일
if exist(File, 'file') == 0
error('File does not exist: %s', File);
end
try
Data = load(File, '-MAT');
catch
Data = load(File, '-ASCII');
end

추가 답변 (1개)

dpb
dpb 2013년 10월 7일
편집: dpb 2013년 10월 7일
Simplest would be if your application that created the files were consistent in naming them such that the .mat extension were to be used only for .mat files and did invariably do so--then simply load filename w/o worrying about the type switch would automagically do "the right stuff".
If you can't do the obvious and expedient, you can build a function that looks at the file with whos -- sotoo
function ismat = ismatfile(fn)
% return logical T if fn is mat-file, F otherwise
try
ismat=~isempty(whos('-file',fn));
catch
ismat=false;
end
end
Above assumes the file does exist; may want to wrap in that test as well depending on your usage.
But, I still think the first suggestion is the better altho this can account for users who don't play by the rules.
  댓글 수: 1
Jan
Jan 2013년 10월 7일
+1: Yes, of course. The best solution to manage the confused file formats is to avoid the confusion by providing different file extensions.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by