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:
Matlab ignores script within script execution

Subject: Matlab ignores script within script execution

From: Tazmusica

Date: 6 Jul, 2009 22:17:01

Message: 1 of 5

Hi,
I have written some scripts to help me deal with data analysis, curve fitting, etc. One such script loads the data, which consists of a series of spectra collected over the course of a set period of time. This script acts on one spectrum at a time, until all of them have been "processed." This is done by calling on a second script within a loop. The second script requests for user input for the curve fits. Once it has this user input, it generates a file to define the fit functions required, and the executes the newly created file to give a cell array of functions for the fit. My problem is that it appears that even though the file is created successfully, it is not always executed (it is apparently ignored in those cases as evidenced by some troubleshooting I have done), and of course this leads to an error when it isn't executed. I am not sure what to do now. The reason that I used the
file to generate the functions is that matlab won't accept other attempts to generate the cell array of functions, even though they look exactly the same (str2funct does not accept the input as valid, etc.). I would appreciate any help, and if needed can provide the scripts, etc. Thanks.

Subject: Matlab ignores script within script execution

From: Gavrilo Bozovic

Date: 7 Jul, 2009 06:26:01

Message: 2 of 5

"Tazmusica " <tazmusica2@deletethis.gmail.com> wrote in message <h2tt4t$qm6$1@fred.mathworks.com>...
> Hi,
> I have written some scripts to help me deal with data analysis, curve fitting, etc. One such script loads the data, which consists of a series of spectra collected over the course of a set period of time. This script acts on one spectrum at a time, until all of them have been "processed." This is done by calling on a second script within a loop. The second script requests for user input for the curve fits. Once it has this user input, it generates a file to define the fit functions required, and the executes the newly created file to give a cell array of functions for the fit. My problem is that it appears that even though the file is created successfully, it is not always executed (it is apparently ignored in those cases as evidenced by some troubleshooting I have done), and of course this leads to an error when it isn't executed. I am not sure what to do now. The reason that I used
the
> file to generate the functions is that matlab won't accept other attempts to generate the cell array of functions, even though they look exactly the same (str2funct does not accept the input as valid, etc.). I would appreciate any help, and if needed can provide the scripts, etc. Thanks.

Could you provide a short example of a code that shows this erratic behavior? don't paste your whole code here, put a relevant subset!

Subject: Matlab ignores script within script execution

From: Tazmusica

Date: 8 Jul, 2009 02:13:01

Message: 3 of 5

Script 1:

DSize=size(intensity.B11);
counter=1;
while counter<=DSize(2)
    XData.B11=ppm.B11(:,counter);
    YData.B11=intensity.B11(:,counter);
    run script_two
    clf
    counter=counter+1;
end

Script 2:

clear funlist;
clear funfiles;
clear LP;
clear NLP;

plot(XData.B11,YData.B11)
set(gca,'XDir','reverse');

%%Get number of Lorentzians from user
nfunct=0;
while nfunct<=0
nfunct=input('How many Lorentzians are you fitting your 11B spectrum to? ');
    if or(nfunct<=0,rem(nfunct,1)~=0)
        disp('The number of functions must be a whole number greater than or equal to 1.');
        nfunct=0;
    end
end

%%Set 11B peakpos to a 1xnfunct matrix of zeros if user does not enter in anything.\n');
peakpos=input('Enter estimates of 11B peak center values. (e.g. [a b c ...] ');
if isempty(peakpos);
    peakpos=zeros(1,nfunct);
end

%%Set 11B fwahh to a 1xnfunct matrix of ones if user does not enter in anything.
fwahh=input('Enter estimates of 11B peak widths at half height. (e.g. [a b c ...] ');
if isempty(fwahh);
    fwahh=ones(1,nfunct);
end

%%Set 11B start_point variable as input to Lorentzian function file
start_point=[];
for sp=1:nfunct;
    start_point=[start_point fwahh(sp) peakpos(sp)];
end

%%Set funlist variable as a list of functions for input to Lorentzian
%%function file

funlist={};
% for funloop=0:1:(nfunct-1)
% funlist{funloop+1}=str2func(['@(c,xdata) (c(' num2str(2*funloop+1) ').^2)./((xdata-c(' num2str(2*funloop+2) ')).^2+(c(' num2str(2*funloop+1) ')).^2)']);
% end

if exist('Funfile.m','file')==2
    delete('Funfile.m')
end

funfiles=fopen('Funfile.m','w'); %Open file here

for funloop=0:1:(nfunct-1)
    fprintf(funfiles,['funlist{' num2str(funloop+1) '}=@(c,xdata) (c(' num2str(2*funloop+1) ').^2)./((xdata-c(' num2str(2*funloop+2) ')).^2+(c(' num2str(2*funloop+1) ')).^2);\n']);
end
fclose(funfiles);
    run Funfile
    funlist
%%Set calculation options
%options=optimset('FunValCheck','on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e-40,'TolFun',1e-40,'disp','iter');
options=optimset('FunValCheck','on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e-40,'TolFun',1e-40);
[NLP,LP,res]=Lorentzian(funlist,start_point,XData.B11,YData.B11,options);

Lorentzian fit script:

%Function to fit data to a sum of n Lorentzians using separable least squares.

%Starting parameters must be chosen carefully!!!

%funlist={@(vshift,xdata) repmat(1,size(YData)),@(c,xdata) ((c(1)).^2./((xdata-c(2)).^2+(c(1)).^2)), @(c,xdata) ((c(3)).^2./((xdata-c(4)).^2+(c(3)).^2)),@(c,xdata) ((c(5)).^2./((xdata-c(6)).^2+(c(5)).^2))};
%start_point=[5 -18 4 -10 2 -6];
%options=optimset('FunValCheck','on','MaxFunEvals',1e30,'MaxIter',1e30,'TolX',1e-40,'TolFun',1e-40);

function[NLP,LP,res]=Lorentzian(funlist,start_point,xdata,ydata,options)


function [sse,LP,res]=Lorentzian_sse(NLP)
    DM=zeros(length(ydata),length(funlist));
    
    for ii=1:length(funlist);
        findx=funlist{ii};
        term=findx(NLP,xdata);
        DM(:,ii)=term(:);
    end
    
    LP=DM\ydata;
    res=DM*LP-ydata;
    sse=sum(res.^2);
    
end

NLP=fminsearch(@Lorentzian_sse,start_point,options);
[crap,LP,res]=Lorentzian_sse(NLP);

Subject: Matlab ignores script within script execution

From: Tazmusica

Date: 8 Jul, 2009 02:39:01

Message: 4 of 5

Hi,
I posted the sections of each script. There are 3 scripts that I provided, the first is the "master script that calls the second one to carry out fit-related stuff, and the 3rd is the actual fit function script. The second script creates a script (Funfile) to define the functions used in the fit. The functions are stored as a cell array in the variable funlist when Funfile is executed. When I use my data, it starts out ok fitting the first few spectra to sums of 2 lorentzians, but when I get to a spectrum that requires to fit to a sum of 3, Funfile does not appear to be executed properly. It appears to be ignoring defining funlist{3}, but it executes everything else. The odd thing is that when I run script_two on its own for each spectrum, I have no problems. I appreciate any help you have to offer.

Subject: Matlab ignores script within script execution

From: Tazmusica

Date: 8 Jul, 2009 21:01:35

Message: 5 of 5

It is probably worth noting that I am using R2008a. I get the same behavior with the scripts under the Windows and Mac versions.

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