Steve Hatcher
Steve Hatcher

Reputation: 715

Save data as modified input file name

I have a program that loads data from a .txt file and performs some curve fitting. The input file name for this example is experiment09.txt.

After processing I want to save a variable with the same input filename but appended with something like _fit. So my saved workspace variable in this case would be experiment09_fit.txt.

I have gotten this far in MATLAB:

buf = length(filename)
saveName = filename(1:buf-7)

which gives me a saveName of experiment09 but I am at a loss as to how to add my chosen string on the end to make it experiment09_fit. Once I have a valid save name I will then just call

save(saveName, 'fittedValue', '-ASCII');

Help would be greatly appreciated.

Upvotes: 0

Views: 94

Answers (2)

PRABHAKARAN
PRABHAKARAN

Reputation: 101

Also use string concatenation for adding additional names to string variables. For example,

    filename = 'experiment09.txt';
    [pathstr, name, ext] = fileparts(filename);
    outputName1 = strcat(name,'_fit.');
    outputName = strcat(outputName1,ext);

Upvotes: 1

Marcin
Marcin

Reputation: 238229

What about this:

filename = 'experiment09.txt';
[pathstr, basename, ext] = fileparts(filename);
outname = [basename, '_fit', ext]; % will give 'experiment09_fit.txt'

Upvotes: 1

Related Questions