Asking the user to enter the extension of the files and load them in workspace

조회 수: 1 (최근 30일)
Here is my code and the question
%%
close all
clc
My_directory=input('Pleas enter your address of the directory containing strongmotion records: ','s');
% Here you are supposed to put your folder name in command window
cd(My_directory);
disp('You are now in the directory containing the strong motion records')
extension=input('Pleas enter the extension of the strongmotion record files: ','s');
I want to write interactive code so that the user can enter his/her own address for the directory containing the files. The above code can take anyone to the directory where the files belong. Now let's assume the directory where you went has many files (it may contain any extension files). Now I want to continue writing the code where the user can enter the extension and load similar files (the input can be m for .m files and txt for .txt files), depending on which files he/she wants to work on.

채택된 답변

Veronica Taurino
Veronica Taurino 2022년 5월 11일
편집: Veronica Taurino 2022년 5월 11일
What do you want to do with those files? Update them as Matlab variables? Store their paths?
However, let's say you want to import them in matlab and loop among them:
My_directory=input('Pleas enter your address of the directory containing strongmotion records: ','s');
% for example: C:\Users\Documents\TEST
cd(My_directory);
disp('You are now in the directory containing the strong motion records')
extension=input('Pleas enter the extension of the strongmotion record files: ','s');
% for example: txt
ext_code =['','*.',extension,''];
directory_all=strcat([My_directory,'\'], ext_code);
files=dir(directory_all);
% loop among files
for file=files'
% do stuff...
% ex. Load data
file_name = file.name
name_complete=strcat([My_directory,'\'],file_name);
DATA = importdata(name_complete)
end
  댓글 수: 1
Chuchu Debebe
Chuchu Debebe 2022년 5월 11일
Thank you, Madam. It helped me much. This is a good answer. Actually, the command load brings those files into the workspace to deal with them.
I looped in this way and it works.
N=length(files);
for i=1:N
filesa_ll=files(i).name;
load(filesa_ll);
end

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

추가 답변 (1개)

cr
cr 2022년 5월 11일
Not sure what you mean by "load similar files". To "load" or import file(s) you first need to specify the file(s). If you want user to first select one or more files of a specified extension, uigetfile() is the way to go. E.g.
uigetfile('*.txt')
if extension is dynamically specified below is how it may be done.
uigetfile(['*.' extension])

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by