필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Hugo
Hugo 2017년 11월 30일
편집: Stephen23 2017년 11월 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에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by