How to input file but file depend to extension

8 ビュー (過去 30 日間)
Matmien
Matmien 2011 年 11 月 26 日
コメント済み: Chuchu Debebe 2022 年 5 月 11 日
%Import data
if nargin==0
filen=load(filename,'mp1')
[filen,path]=uigetfile('*.mat;*.gif',...
'Pick your file');
else
[path,filen,ext]=fileparts(files);
path=[path '\'];
fs=filesep;
if ~isempty(path), path=[path fs]; end
filen={[filen ext]};
end
how to load file automatic?
mean: to load file (just call extension not file name) in specific directory.

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2011 年 11 月 26 日
I have 5 MAT-files and 1 RAR-files in the directory 'Database'. Then I want to load MAT-files only from the directory. This is the code :
clear all; clc;
files = dir(strcat(pwd,'\Database\'));
for x = 1 : size(files,1)
[path{x}, name{x}, ext{x}] = fileparts(files(x).name);
end
for x = 1 : size(files,1)
if strcmp(ext{x},'.mat')
load(strcat(pwd,'\Database\',name{x},'.mat'));
end
end
---------------------------------------------------------
This code is used to load MAT-files only from the selected directory. You can change the directory by changing
files = dir(strcat(pwd,'\Directory\'));
And you also can change the file extention by changing
if strcmp(ext{x},'Any file extention')
and
load(strcat(pwd,'\Directory\',name{x},'Any file extention'));
  1 件のコメント
Chuchu Debebe
Chuchu Debebe 2022 年 5 月 11 日
Good. For my case I want the user to enter the extension in the command window and it loads all those files automatically. It should load in the current directory I am working. No need of changing directory.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by