필터 지우기
필터 지우기

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