Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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");

Risposta accettata

Walter Roberson
Walter Roberson il 18 Ott 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

Più risposte (1)

Image Analyst
Image Analyst il 18 Ott 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.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by