Read a text file with a common name

1 次查看(过去 30 天)
H-H
H-H 2014-10-30
回答: Geoff Hayes 2014-10-30
Hi, I have a program X that generate each time a text file (including some numbers in the file) with a specific name that reflects the current iteration number i.e. "App.in.1", "App.in.2",... . The program X delete the old file (e.g. "App.in.1") after each iteration and creates a new file (e.g. "App.in.2").
I want to have a matlab code that be able to automatically read the current file that include "APP.in." (for example "APP.in.50"), get the iteration number say 50, do some operations, and write back a new file ""APP.out.50". Program X then takes over this text file and deletes it afterwards.
How to write such small matlab code that can read the current file name at time n "App.in.n", and write back a file with name "App.out.n"
Thank you.

回答(1 个)

Geoff Hayes
Geoff Hayes 2014-10-30
If all file names follow this format (iteration number is preceded by App.in.), then just do something like
inPrefix = 'App.in.';
outPrefix = 'App.out.';
currInFilename = 'App.in.50';
currIteration = currInFilename(length(inPrefix)+1:end);
currOutFileName = [outPrefix currIteration];
We rely on the fact that all current iteration files are prefixed with App.in. so we just remove all of this from currInFilename by considering the substring from the index that follows this prefix (so length(inPrefix)+1) to the end of the string in order to extract the iteration number. We then concatenate that string with the output prefix to get
currOutFileName =
App.out.50

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by