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

2 Ansichten (letzte 30 Tage)
Hugo
Hugo am 30 Nov. 2017
Bearbeitet: Stephen23 am 30 Nov. 2017
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,

Akzeptierte Antwort

Stephen23
Stephen23 am 30 Nov. 2017
Bearbeitet: Stephen23 am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by