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

조회 수: 3 (최근 30일)
clear all
clc
a=1; % amplitude du faisceau gaussien
x=linspace(-50,50,100);
dx=50/2.355; % widthn
f=a*exp(-0.5*(x./dx).^2);
[pks, locs, width]=findpeaks(f,x)
plot(x,f)
path='c:/users/user/desktop';
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

채택된 답변

Chunru
Chunru 2021년 11월 24일
a=1; % amplitude du faisceau gaussien
x=linspace(-50,50,100);
dx=50/2.355; % widthn
f=a*exp(-0.5*(x./dx).^2);
[pks, locs, width]=findpeaks(f,x)
pks = 0.9997
locs = -0.5051
width = 47.7723
plot(x,f)
% 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

추가 답변 (2개)

KSSV
KSSV 2021년 11월 24일
path='c:/users/user/desktop';
filename = [path,filesep,'Gauss',num2str(width),'.txt'];
fid = fopen(filename,'w') ;
fprintf(fid,'%f\n',f);
fclose(fid) ;

Mathieu NOE
Mathieu NOE 2021년 11월 24일
hello
try with (last two lines)
f = f';
save(['Gauss' num2str(width) '.txt'], 'f','-ascii')

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by