Clear Filters
Clear Filters

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

2 views (last 30 days)
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,

Accepted Answer

Stephen23
Stephen23 on 30 Nov 2017
Edited: Stephen23 on 30 Nov 2017
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.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!