Changing text file name changes the plot of data after using dir('*.txt') ??

new_files = dir('*.txt');
for file = new_files'
delimiterIn = ' ';
headerlinesIn = 29;
A = importdata(file.name,delimiterIn,headerlinesIn);
% We want to store it into a new 2 column array
% Changing the Depth from A to microns
x1 = A.data(:, 1)/10^4;
y1 = A.data(:, 2);
% New array of ion ranges
ion_range = [x1 y1];
% Fitting data to a gaussian
f = fit(x1,y1,'gauss1');
h = plot(f, '-');
set(h, 'LineWidth', 2);
hold on
end
I have four text files in a folder which I am trying to extract data from, fit the data and plot on the same graph. The text files when titled 40.txt, 80.txt, 160.txt and 250.txt work fine, they result in the expected plot but they do not plot in order meaning the legend on the figure is not in ascending order. So, I read elsewhere that you can change the text file name like so 040.txt, 080.txt, 160.txt and 250.txt, and it will be fixed. But when I do that, the plot of the graph changes and the plots cut off and certain points. Previous to the image shown, when the text files were without the zeros, there were four gaussians plotted.
I cannot work out why this happens - can anyone help?

Answers (2)

Ah. That just happens to work because when the files are renamed, the one with the largest domain gets loaded first.
If they're loaded in natural order, the one with the narrowest domain gets plotted first. Since the way the fit function is plotted without an explicit x input, the subsequent plots are only considered over the domain implied by the current axes extents.
Unless you want to prefetch all the data and find the span of the widest dataset, you can just specify it manually before the first plot:
new_files = dir('*.txt');
for file = new_files'
delimiterIn = ' ';
headerlinesIn = 29;
A = importdata(file.name,delimiterIn,headerlinesIn);
% We want to store it into a new 2 column array
% Changing the Depth from A to microns
x1 = A.data(:, 1)/10^4;
y1 = A.data(:, 2);
% New array of ion ranges
ion_range = [x1 y1];
% Fitting data to a gaussian
f = fit(x1,y1,'gauss1');
xlim([0 0.5]) % <-- this
h = plot(f, '-');
set(h, 'LineWidth', 2);
hold on
end
hello
a "low cost" solution for those who don't have the Curve Fit Toolbox
I force all fitted data to share the same xfit x data - if that may ne of any use / advantage
legend is added , based on file name
all the best
new_files = dir('*.txt');
xmax = 0.5;
xfit = linspace(0,xmax,300);
for ck = 1:numel(new_files)
filename = new_files(ck).name;
delimiterIn = ' ';
headerlinesIn = 29;
A = importdata(filename,delimiterIn,headerlinesIn);
% We want to store it into a new 2 column array
% Changing the Depth from A to microns
x1 = A.data(:, 1)/10^4;
y1 = A.data(:, 2);
% New array of ion ranges
ion_range = [x1 y1];
% Fitting data to a gaussian
yfit = my_gauss_fit(x1,y1,xfit);
h = plot(xfit,yfit, '-');
leg_str{ck} = filename(1:length(filename)-4);
set(h, 'LineWidth', 2);
hold on
end
legend(leg_str);
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function yfit = my_gauss_fit(x,y,xfit)
% Define g(x) = a*exp(-(x-mu)^2/(2*sigma^2)):
g = @(A,X) A(1)*exp(-(X-A(2)).^2/(2*A(3)^2));
%% ---Fit Data: Analitical Strategy---
% Cut Gaussian bell data
ymax = max(y); xnew=[]; ynew=[];
ind = y > 0.1*ymax;
xnew = x(ind);
ynew = y(ind);
% Fitting
ylog=log(ynew); xlog=xnew; B=polyfit(xlog,ylog,2);
% Compute Parameters
sigma=sqrt(-1/(2*B(1))); mu=B(2)*sigma^2; a=exp(B(3)+mu^2/(2*sigma^2));
% Plot fitting curve
A=[a,mu,sigma];
yfit = g(A,xfit);
end

2 Comments

Very helpful as my next problem was to remove the .txt from the legend! Thank you

Sign in to comment.

Asked:

on 4 Feb 2022

Commented:

on 7 Feb 2022

Community Treasure Hunt

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

Start Hunting!