Reading a specific file based on the input the user enters

1 view (last 30 days)
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

Answers (1)

Jon
Jon on 15 Dec 2022
Edited: Jon on 15 Dec 2022
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)

Community Treasure Hunt

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

Start Hunting!