Creating table from Workspace variables within a for loop
Mostra commenti meno recenti
Hello Everyone,
I am rather new to Matlab and right now are running slightly into a wall. I want to fit data from a 3D matrix and extract the fit parameters into tables, i.e. my parameters are a, b, c, d, e etc. and I basically want for each x-y value of my matrix a new matrix with the respective fit parameter for that x-y coordinate. I got so far to get the fitting working, when I write the a from my first loop, that obviously works, but in the second loop the a value of that loop just replaces my a from the first loop, and so on.
The Matlab version I work with is R2014a.
for i=1:512
for j=1:640
y2=A(i,j,:);
y1=y2(:,2:nFiles);
y=transpose(y1);
x1=2 : nFiles;
x=transpose(x1);
try
F = fit(x,y,'a*exp(-x/b)+c*exp(-x/d)+g','Algorithm', 'Levenberg-Marquardt','Robust', 'Bisquare' ,'MaxIter', 50, 'StartPoint', [40 0.5 1 75 0]);
plot(F,x,y);
R = coeffvalues(F);
ParamA = table(R(1:1));
catch ME
end
end
end
1 Commento
Guillaume
il 3 Feb 2020
Why the try...catch? Which line do you expect to fail?
This bit:
y2=A(i,j,:);
y1=y2(:,2:nFiles);
y=transpose(y1);
x1=2 : nFiles;
x=transpose(x1);
is very confusing. Avoid renaming variables. Your A is changed into a y2 which then becomes a y1 (why are we going back in indices?), which then becomes a y. Then we have a x1 that becomes a x. Stick to one variable. On top of that A appears to be a 3D matrix, which is then indexed to produce a 1x1xN matrix. On the next line you ask for all the rows with the : (there is just one!), and column 2:nFiles (there's just one!) and don't index the 3rd dimension (which is where all the elements are now). Now, if you don't index all the dimensions they get lumped with the last dimension indexed, but chances are you didn't know that and your indexing worked by luck. Overall:
y = reshape(A(i, j, 2:end), [], 1); %extract page 2:end at row i, column j,and reshape into column.
would achieve the same and be clearer.
It's unclear what you're trying to achieve with table(A(1:1). Note that A(1:1) is the same as A(1) (or A(1,1,1))
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!