Read a text file with a common name

1 view (last 30 days)
H-H
H-H on 30 Oct 2014
Answered: Geoff Hayes on 30 Oct 2014
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.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Oct 2014
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

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!