Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Accesing data in lsqncurvefit Struct Output

Subject: Accesing data in lsqncurvefit Struct Output

From: Hugh

Date: 11 Nov, 2009 14:58:03

Message: 1 of 5

%% datafitting parameters
tic;
clc
close all
clear screen
data= dlmread('yield.txt','\t');
%data= dlmread('yield2.txt','\t');

ydata=data(2,:);
xdata=data(1,:);

%ydata=data(2:end,:);

%ydata=data(2,:);

k=size(ydata);

% for i=1:k(1)
% xdata(i,:)=data(1,:);
%end

%x=eyes(2:end,:);oi

%xdata=data(1,:);
xdata2=[2 5 10 30];
ydata2=[4.3 4.94 5.52 5.95];
%ydata2=[4.21 4.6725 5.2375 5.715];

xdata3=data(1,:);
ydata3=data(2,:);
%c0 = [1.0 2 3 4];% starting values


%c0 = [1.0 1 1];
%lbn = [-Inf -Inf -Inf -Inf -Inf -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf Inf Inf]; % upper bound

options = optimset('LargeScale','on','MaxFunEvals',10000000,'TolFun',1e-9,'MaxIter',1000000);

z=0;
for (i=0.01:5.01:25.1)

for (j=0.01:5.01:25.1)

for (k=0.01:1.01:5.01)
tic;

c0 = [5.95 1 1 1 i j 0.9 k 0.9 ];
%c0 = [1 1 1 1 1 0.1+i 0.9 0.9 0.9];
%c0 = [4.103594575 21.37812216 -66.97062464 0.079861881 1.81897068 -6.547214299 15.17678637 1.675525953 11.67454445];

%lbn = [-Inf -Inf -Inf -Inf i-0.00000001 j-0.00000001 -Inf k-0.00000001 -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf i+0.00000001 j+0.00000001 Inf k+0.00000001 Inf];
lbn=[];
ubn=[];

z=z+1
% datafitting
%[cn,error]=lsqcurvefit(@PrimePerm,c0,xdata,ydata,lbn,ubn,options);
%[cn,error]=lsqcurvefit(@PrimePerm2,c0,xdata,ydata,lbn,ubn,options);

%[cn,error]=lsqcurvefit(@PrimePerm3,c0,xdata3,ydata3,lbn,ubn,options);
[cn,error,diff,exitf,optdata]=lsqcurvefit(@PrimePerm3,c0,xdata,ydata,lbn,ubn,options);

y=PW(xdata2,ydata2,cn(5),cn(6),cn(7),cn(8),cn(9));
params = y';
%params= [cn(1) cn(2) cn(3) cn(4) ];
params=[params cn(5) cn(6) cn(7) cn(8) cn(9)];
y=CRM(xdata3,params);
error2=(y-ydata3).^2;
error2 = sum(error2);

%Res(1,:) = [cn params error error2];

toc

Res(z,:) = [c0 cn params y error error2 toc];
Res2(z,:) =[exitf optdata.firstorderopt optdata.iterations optdata.funcCount optdata.cgiterations optdata.algorithm optdata.message ];
end
end
end

optim=find(Res(:,43)==min(Res(:,43)));

toc;

Res2 =
  large-scale: trust-region reflective NewtonOptimization terminated: norm of the current step is less
 than OPTIONS.TolX.

Why do I not get the full set of information back?

I want to be able to transfer the data in Res 2 to excel, for all the other variables I just do this by cutting i n pasting from the variable editor. However, when I try to look at Res2 in variable editor it just shows a black cell? Can you tell me how to export this data to excel?

Subject: Accesing data in lsqncurvefit Struct Output

From: Steven Lord

Date: 11 Nov, 2009 15:44:14

Message: 2 of 5


"Hugh " <h_a_patience@hotmail.com> wrote in message
news:hdejdr$gi5$1@fred.mathworks.com...

*snip*

> Res(z,:) = [c0 cn params y error error2 toc];
> Res2(z,:) =[exitf optdata.firstorderopt optdata.iterations
> optdata.funcCount optdata.cgiterations optdata.algorithm
> optdata.message ];
> end
> end
> end
>
> optim=find(Res(:,43)==min(Res(:,43)));
>
> toc;
>
> Res2 =
>   large-scale: trust-region reflective NewtonOptimization terminated:
> norm of the current step is less
> than OPTIONS.TolX.
>
> Why do I not get the full set of information back?

You do ... just not in the form you expected.

> I want to be able to transfer the data in Res 2 to excel, for all the
> other variables I just do this by cutting i n pasting from the variable
> editor. However, when I try to look at Res2 in variable editor it just
> shows a black cell? Can you tell me how to export this data to excel?

When you concatenate together character data and non-character data using
[], as you did when you constructed Res2, the non-character data is
converted into characters.

newstring = ['This string contains the character A: ' 65] % 65 is the ASCII
value for 'A'

So all but the last two components of the optdata structure that you
combined are converted into characters, and since most of them should be
small the characters into which they were converted are ASCII control
characters:

http://en.wikipedia.org/wiki/ASCII#ASCII_control_characters

You probably want to create Res2 as a cell array, not a vector.

newcellstr = {'This cell array contains the number 65: ', 65}

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Accesing data in lsqncurvefit Struct Output

From: Hugh

Date: 11 Nov, 2009 16:10:07

Message: 3 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <hdem2h$68c$1@fred.mathworks.com>...
>
> "Hugh " <h_a_patience@hotmail.com> wrote in message
> news:hdejdr$gi5$1@fred.mathworks.com...
>
> *snip*
>
> > Res(z,:) = [c0 cn params y error error2 toc];
> > Res2(z,:) =[exitf optdata.firstorderopt optdata.iterations
> > optdata.funcCount optdata.cgiterations optdata.algorithm
> > optdata.message ];
> > end
> > end
> > end
> >
> > optim=find(Res(:,43)==min(Res(:,43)));
> >
> > toc;
> >
> > Res2 =
> >   large-scale: trust-region reflective NewtonOptimization terminated:
> > norm of the current step is less
> > than OPTIONS.TolX.
> >
> > Why do I not get the full set of information back?
>
> You do ... just not in the form you expected.
>
> > I want to be able to transfer the data in Res 2 to excel, for all the
> > other variables I just do this by cutting i n pasting from the variable
> > editor. However, when I try to look at Res2 in variable editor it just
> > shows a black cell? Can you tell me how to export this data to excel?
>
> When you concatenate together character data and non-character data using
> [], as you did when you constructed Res2, the non-character data is
> converted into characters.
>
> newstring = ['This string contains the character A: ' 65] % 65 is the ASCII
> value for 'A'
>
> So all but the last two components of the optdata structure that you
> combined are converted into characters, and since most of them should be
> small the characters into which they were converted are ASCII control
> characters:
>
> http://en.wikipedia.org/wiki/ASCII#ASCII_control_characters
>
> You probably want to create Res2 as a cell array, not a vector.
>
> newcellstr = {'This cell array contains the number 65: ', 65}
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>


Thank you so so much!!!

I've amended the code but hit another glitch

 
%% datafitting parameters
tic;
clc
close all
clear screen
data= dlmread('yield.txt','\t');
%data= dlmread('yield2.txt','\t');

ydata=data(2,:);
xdata=data(1,:);

%ydata=data(2:end,:);

%ydata=data(2,:);

k=size(ydata);

% for i=1:k(1)
% xdata(i,:)=data(1,:);
%end

%x=eyes(2:end,:);oi

%xdata=data(1,:);
xdata2=[2 5 10 30];
ydata2=[4.3 4.94 5.52 5.95];
%ydata2=[4.21 4.6725 5.2375 5.715];

xdata3=data(1,:);
ydata3=data(2,:);
%c0 = [1.0 2 3 4];% starting values


%c0 = [1.0 1 1];
%lbn = [-Inf -Inf -Inf -Inf -Inf -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf Inf Inf]; % upper bound

options = optimset('LargeScale','on','MaxFunEvals',10000000,'TolFun',1e-9,'MaxIter',1000000);

z=0;
for (i=0.01:5.01:25.1)

for (j=0.01:5.01:25.1)

for (k=0.01:1.01:5.01)
tic;

c0 = [5.95 1 1 1 i j 0.9 k 0.9 ];
%c0 = [1 1 1 1 1 0.1+i 0.9 0.9 0.9];
%c0 = [4.103594575 21.37812216 -66.97062464 0.079861881 1.81897068 -6.547214299 15.17678637 1.675525953 11.67454445];

%lbn = [-Inf -Inf -Inf -Inf i-0.00000001 j-0.00000001 -Inf k-0.00000001 -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf i+0.00000001 j+0.00000001 Inf k+0.00000001 Inf];
lbn=[];
ubn=[];

z=z+1
% datafitting
%[cn,error]=lsqcurvefit(@PrimePerm,c0,xdata,ydata,lbn,ubn,options);
%[cn,error]=lsqcurvefit(@PrimePerm2,c0,xdata,ydata,lbn,ubn,options);

%[cn,error]=lsqcurvefit(@PrimePerm3,c0,xdata3,ydata3,lbn,ubn,options);
[cn,error,diff,exitf,optdata]=lsqcurvefit(@PrimePerm3,c0,xdata,ydata,lbn,ubn,options);

y=PW(xdata2,ydata2,cn(5),cn(6),cn(7),cn(8),cn(9));
params = y';
%params= [cn(1) cn(2) cn(3) cn(4) ];
params=[params cn(5) cn(6) cn(7) cn(8) cn(9)];
y=CRM(xdata3,params);
error2=(y-ydata3).^2;
error2 = sum(error2);

%Res(1,:) = [cn params error error2];

toc

Res(1,:) = [c0 cn params y error error2 toc];
Res2(1,:) = [exitf];
Res3(1,:) = [optdata.firstorderopt];
Res4(1,:) = [optdata.iterations];
Res5(1,:) = [optdata.funcCount];
Res6(1,:) = [optdata.cgiterations];
Res7(z,:) = [optdata.algorithm];
Res8(z,:) = [optdata.message];
Res9(z,:) = [Res Res2 Res3 Res4 Res5 Res6];

end
end
end

optim=find(Res(:,43)==min(Res(:,43)));

toc;
 
 
 
??? Subscripted assignment dimension mismatch.
Error in ==> CurveGroups at 84
 Res8(z,:) = [optdata.message];

The complier thinks its a subscript problem but I can't understand how. It happened on loop 34 the only difference between this and eaelier loops is that optdata.message is returning a different error message

Subject: Accesing data in lsqncurvefit Struct Output

From: Steven Lord

Date: 11 Nov, 2009 18:33:17

Message: 4 of 5


"Hugh " <h_a_patience@hotmail.com> wrote in message
news:hdenkv$fuv$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <hdem2h$68c$1@fred.mathworks.com>...
>>
>> "Hugh " <h_a_patience@hotmail.com> wrote in message
>> news:hdejdr$gi5$1@fred.mathworks.com...

*snip*

> Thank you so so much!!!

*snip*

> Res(1,:) = [c0 cn params y error error2 toc];
> Res2(1,:) = [exitf];
> Res3(1,:) = [optdata.firstorderopt];
> Res4(1,:) = [optdata.iterations];
> Res5(1,:) = [optdata.funcCount];
> Res6(1,:) = [optdata.cgiterations];

Are you sure these shouldn't be Res(z, :), Res2(z, :), etc. like your Res7,
Res8, and Res9 assignment statements below? Although actually, take a look
below for a better solution.

> Res7(z,:) = [optdata.algorithm];
> Res8(z,:) = [optdata.message];
> Res9(z,:) = [Res Res2 Res3 Res4 Res5 Res6];
>
> end
> end
> end
>
> optim=find(Res(:,43)==min(Res(:,43)));
>
> toc;
>
> ??? Subscripted assignment dimension mismatch.
> Error in ==> CurveGroups at 84
> Res8(z,:) = [optdata.message];
>
> The complier thinks its a subscript problem but I can't understand how. It
> happened on loop 34 the only difference between this and eaelier loops is
> that optdata.message is returning a different error message

All the rows of a matrix must have the same number of columns. As you've
written it, if at one iteration the message is shorter than a previous
message, it will try to fill in the zth row with a string containing fewer
columns than Res8 has.

As I said before, use cell arrays, something like:

Res = cell(numberOfIterations, 9);
for z = 1:numberOfIterations
    % Perform your calls
    Res{z, 1} = [c0 cn params ...
    Res{z, 2} = exitf;
    Res{z, 3} = ...
    ...
    Res{z, 8} = optdata.message;
    ...
end

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Accesing data in lsqncurvefit Struct Output

From: Hugh

Date: 20 Nov, 2009 10:01:03

Message: 5 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <hdevvf$pmg$1@fred.mathworks.com>...
>
> "Hugh " <h_a_patience@hotmail.com> wrote in message
> news:hdenkv$fuv$1@fred.mathworks.com...
> > "Steven Lord" <slord@mathworks.com> wrote in message
> > <hdem2h$68c$1@fred.mathworks.com>...
> >>
> >> "Hugh " <h_a_patience@hotmail.com> wrote in message
> >> news:hdejdr$gi5$1@fred.mathworks.com...
>
> *snip*
>
> > Thank you so so much!!!
>
> *snip*
>
> > Res(1,:) = [c0 cn params y error error2 toc];
> > Res2(1,:) = [exitf];
> > Res3(1,:) = [optdata.firstorderopt];
> > Res4(1,:) = [optdata.iterations];
> > Res5(1,:) = [optdata.funcCount];
> > Res6(1,:) = [optdata.cgiterations];
>
> Are you sure these shouldn't be Res(z, :), Res2(z, :), etc. like your Res7,
> Res8, and Res9 assignment statements below? Although actually, take a look
> below for a better solution.
>
> > Res7(z,:) = [optdata.algorithm];
> > Res8(z,:) = [optdata.message];
> > Res9(z,:) = [Res Res2 Res3 Res4 Res5 Res6];
> >
> > end
> > end
> > end
> >
> > optim=find(Res(:,43)==min(Res(:,43)));
> >
> > toc;
> >
> > ??? Subscripted assignment dimension mismatch.
> > Error in ==> CurveGroups at 84
> > Res8(z,:) = [optdata.message];
> >
> > The complier thinks its a subscript problem but I can't understand how. It
> > happened on loop 34 the only difference between this and eaelier loops is
> > that optdata.message is returning a different error message
>
> All the rows of a matrix must have the same number of columns. As you've
> written it, if at one iteration the message is shorter than a previous
> message, it will try to fill in the zth row with a string containing fewer
> columns than Res8 has.
>
> As I said before, use cell arrays, something like:
>
> Res = cell(numberOfIterations, 9);
> for z = 1:numberOfIterations
> % Perform your calls
> Res{z, 1} = [c0 cn params ...
> Res{z, 2} = exitf;
> Res{z, 3} = ...
> ...
> Res{z, 8} = optdata.message;
> ...
> end
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Hi Steve,

I made the amendments re your earlier post, however, all that the cells in Res return is "[]". What as I doing wrong? (the altered code is shown below)



%% datafitting parameters
tic;
clc
close all
clear screen
%data= dlmread('yield.txt','\t');
data= dlmread('yield2.txt','\t');

ydata=data(2,:);
xdata=data(1,:);

%ydata=data(2:end,:);

%ydata=data(2,:);

%k=size(ydata);


%for i=1:k(1)
% xdata(i,:)=data(1,:);
% end

%x=eyes(2:end,:);oi

%xdata=data(1,:);
xdata2=[2 5 10 30];
%ydata2=[4.3 4.94 5.52 5.95];
ydata2=[4.21 4.6725 5.2375 5.715];

xdata3=data(1,:);
ydata3=data(2,:);
%c0 = [1.0 2 3 4];% starting values


%c0 = [1.0 1 1];
%lbn = [-Inf -Inf -Inf -Inf -Inf -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf Inf Inf]; % upper bound

options = optimset('LargeScale','off','MaxFunEvals',1000000,'TolFun',1e-5,'MaxIter',1000000);

z=0;
for (i=0.01:5.01:25.1)

for (j=0.01:5.01:25.1)

for (k=0.01:1.01:5.01)
tic;

c0 = [5.95 1 1 1 i j 0.9 k 0.9 ];
%c0 = [1 1 1 1 1 0.1+i 0.9 0.9 0.9];
%c0 = [4.103594575 21.37812216 -66.97062464 0.079861881 1.81897068 -6.547214299 15.17678637 1.675525953 11.67454445];

%lbn = [-Inf -Inf -Inf -Inf i-0.00000001 j-0.00000001 -Inf k-0.00000001 -Inf]; % lower bound
%ubn = [Inf Inf Inf Inf i+0.00000001 j+0.00000001 Inf k+0.00000001 Inf];
lbn=[];
ubn=[];

z=z+1
% datafitting
%[cn,error]=lsqcurvefit(@PrimePerm,c0,xdata,ydata,lbn,ubn,options);
%[cn,error]=lsqcurvefit(@PrimePerm2,c0,xdata,ydata,lbn,ubn,options);

%[cn,error]=lsqcurvefit(@PrimePerm3,c0,xdata3,ydata3,lbn,ubn,options);
[cn,error,diff,exitf,optdata]=lsqcurvefit(@PrimePerm3,c0,xdata,ydata,lbn,ubn,options);

y=PW(xdata2,ydata2,cn(5),cn(6),cn(7),cn(8),cn(9));
params = y';
%params= [cn(1) cn(2) cn(3) cn(4) ];
params=[params cn(5) cn(6) cn(7) cn(8) cn(9)];
y=CRM(xdata3,params);
error2=(y-ydata3).^2;
error2 = sum(error2);

%Res(1,:) = [cn params error error2];

toc

Res=cell(180,9);


% Res(1,:) = [c0 cn params y error error2 toc];
% Res2(1,:) = [exitf];
% Res3(1,:) = [optdata.firstorderopt];
% Res4(1,:) = [optdata.iterations];
% Res5(1,:) = [optdata.funcCount];
% Res6(1,:) = [optdata.cgiterations];
% Res7(z,:) = [optdata.algorithm];
% Res8(z,:) = [optdata.message];
% Res9(z,:) = [Res Res2 Res3 Res4 Res5 Res6];

Inpts(z,:) = [c0 cn params y error error2 toc];
Res{z,1} = exitf;
Res{z,2} = optdata.firstorderopt;
Res{z,3} = optdata.iterations;
Res{z,4} = optdata.funcCount;
%Res6(1,:) = [optdata.cgiterations];
Res{z,5} = optdata.stepsize;
Res{z,6} = optdata.algorithm;
Res{z,7} = optdata.message;
%Res9(z,:) = [Res Res2 Res3 Res4 Res5 Res6];

end
end
end

%optim=find(Res{:,43}==min(Res{:,43}));

toc;

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us