Reading a specific file based on the input the user enters

조회 수: 3 (최근 30일)
Inês Silva
Inês Silva 2022년 12월 15일
편집: Jon 2022년 12월 15일
Hi,
I have a folder with several .txt files whose names are "e01Demo.txt" , "e02Demo.txt", "e03Demo.txt" and so on. And I wish to create a program where based on a number the user inserts ( using an input ), so, for example, 01, the programme will read the file that correspondes to that number, which in this case wouls be "e01Demo.txt".
I have tried several diferent functions such as dir and uigetfile but none of them allow me to read a different file depending on the input a future user might chose to insert.
Thank you

답변 (1개)

Jon
Jon 2022년 12월 15일
편집: Jon 2022년 12월 15일
You could do something like this
% prompt user for test series number
seriesNo = inputdlg('Please enter the test series number','Enter series')
% convert to numerical value
seriesNo = str2num(seriesNo{1})
% make it into a string with leading zero
s = num2str(seriesNo,'%02d')
% read the appropriate file
dat = readmatrix(['e',s,'Demo.txt'])
Another approach would be to use uigetfile to display a directory listing of the available files and just let the user pick the file they wanted
Like this
file = uigetfile('e*demo.txt')
dat = readmatrix(file)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by