Question

I have the code mentioned below in matlab. I want to write all the 162 rows and 4 columns calculated into an excel file.

When i use xlswrite in the code i get only one row and 4 columns as the value of P gets overwritten in each iterative step.

If i use another loop inside the for loop the execution time increase drastically. Please help to least write the values of P into an array which i can later write into excel file(when i tried 'In an assignment A(I) = B, the number of elements in B and I must be the same' error appeared.)

please help

function FitSMC_BC
clc
% Parameters: P(1)=theta_S; P(2)=theta_r; P(3)=psib; P(4)=lamda; 
smcdata=xlsread('asimdata');
nn=length(smcdata)-1;

for i=1:nn
    psi=smcdata(:,1);
    thetaObs=smcdata(:,i+1);
    %Make an initial guess:
    Pini=[0.5 0.1 -1 1.5];
    P=fminsearch(@ObFun,Pini,[],psi,thetaObs);
    disp(['result',num2str(i),': P=',num2str(P)]);
    theta=Gettheta(P,psi);
end

function OF=ObFun(P,psi,thetaObs)
theta=Gettheta(P,psi);
OF=sqrt(mean((theta - thetaObs).^2));

function theta=Gettheta(P,psi)
SoilPars.theta_S=P(1);
SoilPars.theta_r=P(2);
SoilPars.psib=P(3);
SoilPars.lamda=P(4);
[theta]=thetaFun(psi,SoilPars);

function [theta]=thetaFun(psi,SoilPars)
theta_S=SoilPars.theta_S;
theta_r=SoilPars.theta_r;
psib=SoilPars.psib;
lamda=SoilPars.lamda;
theta=theta_r+((theta_S-theta_r)*((psib./psi).^lamda));
theta(psi>psib)=theta_S;
Was it helpful?

Solution

You can modify the P line with

P(i,:) = fminsearch(@ObFun,Pini,[],psi,thetaObs);

P will store each calculation (4 element vector) in a new line.

You may also initialise P before the for loop with P = nan(nn, 4);

Then write P in an Excel file using xlswrite.

OTHER TIPS

I haven't studied your code in-depth, but as far as I can tell, you have two options:

  1. Create a matrix P and use xlswrite on the entire matrix. This seems to me like the most reasonable approach.
  2. Use xlswrite1 from the fileexchange in a loop. This will increase execution time a bit, but not nearly as much as using regular xlswrite as it is specially deigned to be used inside loops. The reason why it is so much faster is because it only opens and closes the Excel-file once, whereas the regular xlswrite opens and closes it every time you call the function.

You seem to know how to use indexing so I'm not sure why you're simply doing something like this:

P = zeros(size(smcdata,1),nn)
for i=1:nn
    ...
    P(:,i) = fminsearch(@ObFun,Pini,[],psi,thetaObs);
    disp(['result',num2str(i),': P=',num2str(P(:,i))]);
    theta = Gettheta(P(:,i),psi); % Why is this here? Are you writing it to file too?
end
xlswrite('My_FileName.xls',P);

Or you could call xlswrite on each iteration of the loop (probably slower) and append the new data using something like this:

for i=1:nn
    ...
    P = fminsearch(@ObFun,Pini,[],psi,thetaObs);
    disp(['result',num2str(i),': P=',num2str(P)]);
    theta = Gettheta(P,psi); % Why is this here? Are you writing it to file too?
    xlswrite('My_FileName.xls',P,1,['A' int2str((i-1)*size(P,2)+1)]);
end

Of course your code isn't runnable so you'll have to debug any other little errors. Also, since smcdata seems to be a matrix rather than a vector, you should be careful using length with it. You probably should use size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top