Reading a specific file based on the input the user enters

3 Ansichten (letzte 30 Tage)
Inês Silva
Inês Silva am 15 Dez. 2022
Bearbeitet: Jon am 15 Dez. 2022
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

Antworten (1)

Jon am 15 Dez. 2022
Bearbeitet: Jon am 15 Dez. 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)


Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by