Afficher commentaires plus anciens
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 commentaire
Niki
le 31 Août 2011
Réponses (2)
Walter Roberson
le 31 Août 2011
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 commentaire
Niki
le 31 Août 2011
Fangjun Jiang
le 31 Août 2011
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 commentaires
Niki
le 31 Août 2011
Fangjun Jiang
le 31 Août 2011
Okay, what do you want to do if from workspace?
Niki
le 1 Sep 2011
Fangjun Jiang
le 1 Sep 2011
See edit. You can use uiimport() or importdata()
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!