How to input filename as a variable for function?

hi
I have a program where i need to enter the filename as an input. Currently i have written a progran where i have to input the filename.
I want to create a function where i type enter_filename(filename) and the filename is a file with .txt extension.
So I need to enter something like this test.txt as my input to the function.
Please help me create a function where I can enter filename test.txt and execute my program.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 28 日
編集済み: Azzi Abdelmalek 2012 年 9 月 28 日

3 投票

filename=input('enter file name')
or if you want to read an existant file
[file,folder]=uigetfile
filename=fullfile(folder,file)
Image Analyst
Image Analyst 2012 年 9 月 28 日

1 投票

Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the color image file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Now call your other m-file with this filename as an input argument:
[results1 results2] = yourOtherMFile(fullFileName);
Of course, I don't know what your other m-file is doing with the filename or what values it's returning, so you have to write that part.

2 件のコメント

Abdur Rahman
Abdur Rahman 2020 年 2 月 26 日
How do I read multiple file at the same time?
Image Analyst
Image Analyst 2020 年 2 月 27 日
Normally you read only one file at a time. To read multiple files at a time I think you'd have to have the Parallel Computing Toolbox so you can have multiple CPU cores working on multiple files simultaneously.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDebugging and Analysis についてさらに検索

タグ

質問済み:

Dee
2012 年 9 月 28 日

コメント済み:

2020 年 2 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by