How do I read a file name into a function?

I have a folder full of .stl files containing sets of (x,y,z) co-ordinates of points in 3d. I have found a function, stlread, which reads these files, all I have to do is call the path of the file in ' ' quotation marks.
How do I choose this file from a directory and put it into stlread? If I use the MATLAB function filename=uigetfile, I can choose the file but if I use stlread(filename) it doesn't work even though filename is a string.
Eventually I wish to possibly have a list of paths to .stl files in a .txt file and then extract each one in turn to run stlread on all of them. Is this possible somehow?

1 件のコメント

Sara
Sara 2014 年 8 月 4 日
Look into dir(directory_name). This will give you the list of all the files in a specific directory. You can then run your code on all the stl files with a loop.

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

 採用された回答

Dasharath Gulvady
Dasharath Gulvady 2014 年 8 月 4 日
編集済み: Dasharath Gulvady 2014 年 8 月 4 日

2 投票

In order to read all the files with file names matching a specific pattern, you can use "fullfile" function. The below sample code will read all the file names that end with ".stl" in a given directory :
filePattern = fullfile('./', '*.m');
files = dir(filePattern);
Here is the documentation page for dir .
Note that './' refers to the current directory and it can be changed to any other directory.
Note that "uigetfile" will let you select a file without navigating to the folder in which the file is present.
Now you can loop through "files" to get the filenames using the "name" field of the structure "files". A demo code is as follows:
for i=1:length(files)
filename = files(i);
filename.name
%stlread(filename); %you can try this out by uncommenting
end
Also, to read file names from a text file, you can use textread .
Also take a look at few other such functions here which may come be helpful.

1 件のコメント

Andrii Mazur
Andrii Mazur 2018 年 6 月 16 日
移動済み: DGM 2025 年 7 月 25 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2014 年 8 月 4 日

移動済み:

DGM
2025 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by