I am trying to have a user input a part of a file name which will open the whole file.

2 次查看(过去 30 天)
e.g. the file name is Track P56.csv and i want to just be able to type in 56 when prompted to input a Track number.
This is how it is done currently however i have to type 'Track P56.csv' instead of just the number 56. Thank you in advance.
%User Input for Track Number
tracknum = input("Track file name, Format: ('Track P__.csv'): ");
%Opening the File
fid = fopen(tracknum,"r");

采纳的回答

Walter Roberson
Walter Roberson 2023-10-18
trackfile = "Track P" + tracknum + ".csv";
[fid, msg] = fopen(trackfile, "r");
if fid < 0
error('Could not open file "%s" because "%s"', trackfile, msg);
end

更多回答(1 个)

Image Analyst
Image Analyst 2023-10-18
Try this:
tracknum = input("Track file name, Format: ('Track P__.csv'): ");
folder = pwd; % Wherever you want.
fullFileName = fullfile(folder, sprintf('Track P%d.csv', tracknum));
%fid = fopen(fullFileName,"rt");
if isfile(fullFileName)
% File exists so read in the data.
data = readmatrix(fullFileName) % Read in the CSV file.
else
% File does not exist. Alert the user.
errorMessage = sprintf('File not found: "%s"', fullFileName)
uiwait(errordlg(errorMessage));
return;
end
Better would be to load up the files into a listbox on a GUI and let the user click on the one they want.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by