How can I open a .txt file in a directory, regardless of its name??

2 次查看(过去 30 天)
Hi,
I am coupling MATLAB with ANSYS for structural optimization and I need to open an output file generated by ANSYS. The problem is that ANSYS gives the last output, but the output filename is different in each iteration, as it in the format xxx.txt, where xxx is the last substep. The problem is that the number of substeps can vary from 100 to 400. I must read the output file with a MATLAB code, however, I can only define the output as a specific file (such as 100.txt or 200.txt). How can I tell MATLAB to open the only .txt file in a given directory, regardless of its name? I am defining a function, and I call the file 100.txt. How can I change it to read whatever file with .txt extension is in that directory? A sample of the code is:
Problem.ansys.output_file = 'C:\Users\Johnny\Desktop\blow_moulding_2\blow_moulding_viscoelastic\520_files\dp0\PFL-BM\PFL\Outputs\xml\100.txt';
Best regards,

采纳的回答

Stephen23
Stephen23 2017-11-30
编辑:Stephen23 2017-11-30
Use dir to get the filename:
P = 'C:\Users\Johnny\Desktop\blow_moulding_2\blow_moulding_viscoelastic\520_files\dp0\PFL-BM\PFL\Outputs\xml';
S = dir(fullfile(P,'*.txt'));
Z = fullfile(P,S.name)
You should of course check how many files it identifies, and handle the cases 0 and >1.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by