Clear Filters
Clear Filters

Read multiple files and store fitting parameters & graphs

1 view (last 30 days)
I have a function that reads from an input file (floating point numbers, only) columns (A, B, C, D, E, F, G, H, I) perform some mathematical operations and plot some fittings.
Since I have multiple input files named as: res1, res8, ..., resn, attention not in sequencial order (meaning not having res2, for example) how should I do for my script read all those input files and for each run/analysis create an output file results1, results8 ... etc, with fitting results and plots?
Any help would be much appreciated.
  4 Comments
Bob Thompson
Bob Thompson on 12 Feb 2019
You're only getting one set of outputs because you're only looking at mydata{k}, where k is numfiles (the last one). If you want to perform the operation for all of the files, move them all inside the loop, including the output.

Sign in to comment.

Answers (2)

Bob Thompson
Bob Thompson on 12 Feb 2019
function [results] = test(A,B,C,D,E,F,G,H,time)
numfiles = 2;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('Res%d.txt', k);
mydata{k} = importdata(myfilename);
A = mydata{k}(:,1);
B = mydata{k}(:,2);
C = mydata{k}(:,3);
D = mydata{k}(:,4);
E = mydata{k}(:,5);
F = mydata{k}(:,6);
G = mydata{k}(:,7);
H = mydata{k}(:,8);
time = mydata{k}(:,9);
ABsub = E - E(1);
BAsub = G - G(1);
ref = A(1) + C(1);
AA = A/ref;
BB = B/ref;
AB = ABsub/ref;
BA = BAsub/ref;
% Insert new database creation here
% Insert output command here
end

Angelo Figueiredo
Angelo Figueiredo on 13 Feb 2019
Sorry once again but I still don't understand why I am getting one plot from my dataset. Besides how can I create at the end of the running script an output file with all fitting results for all files executed
function [results] = test(A,B,C,D,E,F,G,H,time)
numfiles = 2;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('Res%d.txt', k);
mydata{k} = importdata(myfilename);
A = mydata{k}(:,1);
B = mydata{k}(:,2);
C = mydata{k}(:,3);
D = mydata{k}(:,4);
E = mydata{k}(:,5);
F = mydata{k}(:,6);
G = mydata{k}(:,7);
H = mydata{k}(:,8);
time = mydata{k}(:,9);
ABsub = E - E(1);
BAsub = G - G(1);
ref = A(1) + C(1);
AA = A/ref;
BB = B/ref;
AB = ABsub/ref;
BA = BAsub/ref;
% Insert new database creation here
% Insert output command here
[AAcol,AArow] = size(normAA);
if AArow > AAcol
normAA = normAA';
end
[BBcol,BBrow] = size(normBB);
if BBrow > BBcol
normBB = normBB';
end
end
end
%estimated values
default.k = 3.2;
default.pA = 0.5;
default = [default.k;default.pA];
[fitparameters] = fminsearch(@(x) zex(x,normAA,normBB,time),default);
k = fitparameters(1);
pA = fitparameters(2);
[residual,IAA,IBB] = zex(fitparameters,normAA,normBB,time);
results.k = k;
results.pA = pA;
results.datafit = [normAA normBB];
results.dataIAA = normAA;
results.dataIBB = normBB;
results.fitIAA = IAA;
results.fitIBB = IBB;
figure
hold on
plot(time,normAA,'o','color','r')
box on
fprintf('\n~~~~Results~~~~\nkex: %f\npA: %f\npB: %f\nR1A: %f\nresidual: %f\n',k,pA,residual)
end
function [residual,IAA,IBB] = zex(default,Iaa,Ibb,time)
k = default(1);
pA = default(2);
number_times = length(time);
aAA = zeros(number_times,1);
aBB = zeros(number_times,1);
for n = 1:1:number_times
aAA(n) = pA*(pA+pB*exp(-kex*time(n)))*exp(-R1A*time(n));
end
end
  8 Comments
Bob Thompson
Bob Thompson on 13 Feb 2019
Mmm, I forgot this part. Put brackets around your new structure field definitions.
[default.ex] = 3.2;
[default.pA] = ...
Angelo Figueiredo
Angelo Figueiredo on 16 Feb 2019
Hi again, still I don't understand why I only get one plot and not two plots as read by 'numfiles', as well as only one fitting result output instead of two?

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!