load a data

I know that we can load data with different format,
I would like to put in a loop for example if I==1; load .... end
something that if the data is saved in *.txt or *.xls or *.mat or anything which Matlab can read it
the name of the file let say saved as X.
therefore
if I==1 load X load X.txt numeric= xlsread('X'); if I==2 uiimport end end
but I would likein the first step if i==1 , then if the file was one those format, then be loaded by Matlab

댓글 수: 1

Niki
Niki 2011년 8월 31일
maybe this ?
if I==1
load X
elseif
load X.txt
elseif
numeric= xlsread('X');
elseif I==2
uiimport
end
end

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

답변 (2개)

Walter Roberson
Walter Roberson 2011년 8월 31일

0 개 추천

loadworked = false;
try
load(X)
loadworked = true;
end
if ~loadworked
try
uiimport(X);
loadworked = true;
end
end
if ~loadworked
error('Bleh, could not load or import the file!')
return
end

댓글 수: 1

Niki
Niki 2011년 8월 31일
Thanks Walter but I would like to put 1 or 2
because i use of input ('Import data=> from file=1, from workspace=2 ? ');

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

Fangjun Jiang
Fangjun Jiang 2011년 8월 31일

0 개 추천

importdata() automatically does that. What is your purpose? To learn if-else statement?
To check the file extension, use fileparts().
I=input('Import data=> from file=1, from workspace=2 ? ');
if I==1
[PathStr,File]=uigetfile;
[Trash,FileName,FileExt]=fileparts(File);
if strcmpi(FileExt,'.txt')
a=textread(File);
elseif strcmpi(FileExt,'.mat')
a=load(File);
elseif strcmpi(FileExt,'.xls')
a=xlsread(File);
else
error('Unrecognized File!');
end
elseif I==2
%do stuff
else
error('Invalid input');
end

댓글 수: 4

Niki
Niki 2011년 8월 31일
Thanks Fangiun, as I said to Walter, I would like to put a command whether the data can be loaded from workspace or from file
then I would like if it is supposed to be loaded from directory (current folder) then if the file is in xls or any other format, it can be loaded without any problem
Fangjun Jiang
Fangjun Jiang 2011년 8월 31일
Okay, what do you want to do if from workspace?
Niki
Niki 2011년 9월 1일
you think I should not mention a while ? because if it is not 1 nor 2 , then the program will stop , No?
therefore it should be something that until I enter 1 or 2 , the program need the input
on the other hand, I would like to read the data from a file
then I use of >>uiimport
I would like the program stay until I load the data using uiimport and then continue and not when I load the data then it stop
Fangjun Jiang
Fangjun Jiang 2011년 9월 1일
See edit. You can use uiimport() or importdata()

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

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

질문:

2011년 8월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by