Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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");

Réponse acceptée

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

Plus de réponses (1)

Image Analyst
Image Analyst le 18 Oct 2023
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.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by