how to save variable as txt in a specified location with variable value appearing in the file name

1 view (last 30 days)
Rabih Sokhen
Rabih Sokhen on 24 Nov 2021
Answered: Mathieu NOE on 24 Nov 2021
clear all
a=1; % amplitude du faisceau gaussien
dx=50/2.355; % widthn
[pks, locs, width]=findpeaks(f,x)
save('Gauss',num2str(width),'.txt', 'f','-ascii')
hello guys
i want to save My "f" variable as a txt file in the path location and i want to name the following file as Gauss.value of width.txt
i tried the following code but i get a error:
error using save '47.7723' is not a valid variable name
error in line 10
thank you in advance for your help

Accepted Answer

Chunru on 24 Nov 2021
a=1; % amplitude du faisceau gaussien
dx=50/2.355; % widthn
[pks, locs, width]=findpeaks(f,x)
pks = 0.9997
locs = -0.5051
width = 47.7723
% don't use "path" as a variable name
% path='c:/users/user/desktop';
folder = ''; % Modify this
save(fullfile(folder, 'Gauss.txt'), 'f', '-ascii')
type Gauss.txt
6.2473520e-02 6.9801253e-02 7.7812158e-02 8.6546338e-02 9.6043268e-02 1.0634135e-01 1.1747741e-01 1.2948623e-01 1.4239994e-01 1.5624747e-01 1.7105399e-01 1.8684023e-01 2.0362195e-01 2.2140926e-01 2.4020606e-01 2.6000945e-01 2.8080918e-01 3.0258714e-01 3.2531691e-01 3.4896333e-01 3.7348223e-01 3.9882014e-01 4.2491418e-01 4.5169196e-01 4.7907167e-01 5.0696225e-01 5.3526363e-01 5.6386723e-01 5.9265638e-01 6.2150706e-01 6.5028864e-01 6.7886476e-01 7.0709433e-01 7.3483264e-01 7.6193254e-01 7.8824568e-01 8.1362384e-01 8.3792034e-01 8.6099136e-01 8.8269740e-01 9.0290467e-01 9.2148643e-01 9.3832434e-01 9.5330972e-01 9.6634466e-01 9.7734315e-01 9.8623201e-01 9.9295167e-01 9.9745685e-01 9.9971711e-01 9.9971711e-01 9.9745685e-01 9.9295167e-01 9.8623201e-01 9.7734315e-01 9.6634466e-01 9.5330972e-01 9.3832434e-01 9.2148643e-01 9.0290467e-01 8.8269740e-01 8.6099136e-01 8.3792034e-01 8.1362384e-01 7.8824568e-01 7.6193254e-01 7.3483264e-01 7.0709433e-01 6.7886476e-01 6.5028864e-01 6.2150706e-01 5.9265638e-01 5.6386723e-01 5.3526363e-01 5.0696225e-01 4.7907167e-01 4.5169196e-01 4.2491418e-01 3.9882014e-01 3.7348223e-01 3.4896333e-01 3.2531691e-01 3.0258714e-01 2.8080918e-01 2.6000945e-01 2.4020606e-01 2.2140926e-01 2.0362195e-01 1.8684023e-01 1.7105399e-01 1.5624747e-01 1.4239994e-01 1.2948623e-01 1.1747741e-01 1.0634135e-01 9.6043268e-02 8.6546338e-02 7.7812158e-02 6.9801253e-02 6.2473520e-02

More Answers (2)

KSSV on 24 Nov 2021
filename = [path,filesep,'Gauss',num2str(width),'.txt'];
fid = fopen(filename,'w') ;
fclose(fid) ;

Mathieu NOE
Mathieu NOE on 24 Nov 2021
try with (last two lines)
f = f';
save(['Gauss' num2str(width) '.txt'], 'f','-ascii')


Community Treasure Hunt

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

Start Hunting!