Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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");

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Okt. 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

Weitere Antworten (1)

Image Analyst
Image Analyst am 18 Okt. 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.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by