Read a text file with a common name

1 visualizzazione (ultimi 30 giorni)
H-H
H-H il 30 Ott 2014
Risposto: Geoff Hayes il 30 Ott 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.

Risposte (1)

Geoff Hayes
Geoff Hayes il 30 Ott 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

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by