DEPLOYED MATLAB FILE DOES NOT START - matlab

Please, I have this piece of code that involves saving state of gui (using Doug Hull's) approach.
The problem is the script compiles alright but only pops up and vanishes when I run it, using deploytool. I am using Matlab R2012b.
function savestate3
S.fh=figure('NumberTitle','off',...
'Visible','on','Position',[360 400 450 285],...
'closerequestfcn',{#fh_crfcn});
S.tg(1)=uicontrol(S.fh,'Style','toggle','String','Semester',...
'pos',[15 250 100 25],'val',0,'visible','on');
S.tg(2)=uicontrol(S.fh,'Style','toggle','String','Details',...
'pos',[135 250 100 25],'val',0,'visible','on');
S.ed(1)=uicontrol(S.fh,'Style','edit','String','Edit Text',...
'pos',[250 70 100 25],'visible','off');
S.lb(1)=uicontrol(S.fh,'Style','listbox','String',{'One','Two','Three'},...
'pos',[100 170 100 70],'visible','off');
S.cb(1)=uicontrol(S.fh,'Style','checkbox','Value',1,'String','Check Me',...
'pos',[250 170 100 25],'visible','off');
set(S.tg(:),'callback',{#tg_call,S})
guidata(S.fh,S)
restoreState(S);
function saveState(handles)
state2.editstr=get(S.ed(1),'String');
state2.listval=get(S.lb(1),'value');
state2.checkval=get(S.cb(1),'value');
save state1.mat state2
end
function restoreState(handles)
load 'state1.mat' 'state2'
set(S.ed(1),'string',state2.editstr,'FontSize',12,'FontWeight','bold');
set(S.lb(1),'value',state2.listval);
set(S.cb(1),'value',state2.checkval);
end
function fh_crfcn(varargin)
saveState(S)
delete(S.fh)
end
%TOGGLE OPERATIONS
function []=tg_call(varargin)
%Toggle Operations
[h,S]=varargin{[1,3]};
if get(h,'val')==0
set(h,'val',1)
end
switch h
case S.tg(1)
set(S.tg(2),'val',0)
set(S.ed(1),'visible','on')
set(S.lb(1),'visible','on')
set(S.cb(1),'visible','off')
saveState(S)
case S.tg(2)
set(S.tg(1),'val',0)
set(S.cb(1),'visible','on')
set(S.ed(1),'visible','off')
set(S.lb(1),'visible','off')
saveState(S)
end
end
end

Well I figured this out and still don't know if that is the 'perfect' thing to do.
After compiling, I put the mat file state1.mat in the distr/src folder and it worked.

Related

Find some numbers in Matlab satisfying a bunch of inequalities

I want to find some numbers in Matlab (denominated below p11,..., p119) satisfying a bunch of inequalities (specifically, 16 inequalities). I want Matlab to keep searching until it finds such numbers. I thought about using while as below but it does no work. What is wrong? How can I proceed?
clear
rng default
%% SOME INITIAL VALUES
p11=0.3;
p12=0.4;
p13=0.1;
p14=0.2;
p15=0.2;
p16=0.2;
p17=0.06;
p18=0.03;
p19=0.02;
p110=0.04;
p111=0.07;
p112=50;
p113=0.02;
p114=0.03;
p115=0.01;
p116=0.08;
p117=0.01;
p118=0.1;
p119=0.07;
while ... %CONDITION THAT SHOULD BE SATISFIED (16 CONDITIONS)
((p11<=(p15+p19+p110+p111+p115+p116+p117+p119))+...
(p12<=(p16+p19+p112+p113+p115+p117+p118+p119))+...
(p13<=(p17+p110+p112+p114+p116+p117+p118+p119))+...
(p14<=(p18+p111+p113+p114+p115+p116+p118+p119))+...
(p11+p12<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p16+p112+p113+p118))+...
(p11+p13<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p17+p112+p114+p118))+...
(p11+p14<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p18+p113+p114+p118))+...
(p12+p13<=(p16+p19+p112+p113+p115+p117+p118+p119+...
p17+p110+p114+p116))+...
(p12+p14<=(p16+p19+p112+p113+p115+p117+p118+p119+...
p18+p111+p114+p116))+...
(p13+p14<=(p17+p110+p112+p114+p116+p117+p118+p119+...
p18+p111+p113+p115))+...
(p11+p12+p13<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p16+p112+p113+p118+...
p17+p114))+...
(p11+p12+p14<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p16+p112+p113+p118+...
p18+p114))+...
(p11+p13+p14<=(p15+p19+p110+p111+p115+p116+p117+p119+...
p17+p112+p114+p118+...
p18+p113))+...
(p12+p13+p14<=(p16+p19+p112+p113+p115+p117+p118+p119+...
p17+p110+p114+p116+...
p18+p111))+...
(p11+p12+p13+p14==1)+...
(p15+p16+p17+p18+p19+p110+p111+p112+p113+p114+p115+p116+p117+p118+p119==1))~=15
% IF THE CONDITION IS NOT SATISFIED KEEP SEARCHING BY GUESSING
% OTHER NUMBERS
p11=unifrnd(0,1);
p12=unifrnd(0,1);
p13=unifrnd(0,1);
p14=unifrnd(0,1);
p15=unifrnd(0,1);
p16=unifrnd(0,1);
p17=unifrnd(0,1);
p18=unifrnd(0,1);
p19=unifrnd(0,1);
p110=unifrnd(0,1);
p111=unifrnd(0,1);
p112=unifrnd(0,1);
p113=unifrnd(0,1);
p114=unifrnd(0,1);
p115=unifrnd(0,1);
p116=unifrnd(0,1);
p117=unifrnd(0,1);
p118=unifrnd(0,1);
p119=unifrnd(0,1);
end
The while loop will run while the condition is true. If false it terminates. Your test conditions is while .... ~= 15. This is false as the initial guesses result in 15 out of 16 trues. Since 15 ~= 15 is false, the while loop doesn't run.
One way to fix the issue is to change from ~= to ==. This will run through and find a solution to that condition.
You could have seen this by creating a variable called tests and populated it like this:
tests = [(p11<=(p15+p19+p110+p111+p115+p116+p117+p119));...
... skipped a bunch of stuff ...
(p15+p16+p17+p18+p19+p110+p111+p112+p113+p114+p115+p116+p117+p118+p119==1)];
sum(tests)
ans = 15
Or any other way of tracking that value.

Matlab: efficienting portion of code, random start

I have the following code that generates a matrix of 15 blocks that will then be used in a Montecarlo approach as multiple starting points. How can I get the same exact result in a smarter way?
assume that J=15*100 are the total simulation and paramNum the number of parameters
[10^-10*ones(paramNum,round(J/15)) 10^-9*ones(paramNum,round(J/15)) 10^-8*ones(paramNum,round(J/15)) 10^-7*ones(paramNum,round(J/15)) 10^-6*ones(paramNum,round(J/15)) 10^-5*ones(paramNum,round(J/15)) rand*10^-5*ones(paramNum,round(J/15)) 10^-4*ones(paramNum,round(J/15)) rand*10^-4*ones(paramNum,round(J/15)) 10^-3*ones(paramNum,round(J/15)) 10^-2*ones(paramNum,round(J/15)) 10^-1*ones(paramNum,round(J/15)) 10^-abs(randn/2)*ones(paramNum,round(J/15))];
you could do
v = 10.^[-10:-5 rand*10^-5 -4:-1 10^-abs(randn/2)];
repmat(repelem(v, 1, round(J/15)), paramNum) .* ...
repmat(ones(paramNum,round(J/15)), numel(v))
Or mimic the repmat/repelem functionality with a for loop. The first is shorter, the later is more understandable.
By the way... it's less than 15 blocks...

Getting the number of function calls of fminsearch for each iteration

options = optimset('Display','iter','MaxIter',3,'OutputFcn',#outfun);
[x,fval,~,output] = fminsearch(#(param) esm6(param,identi),result(k,1:end-1),options);
This code will find the local Minimum of my esm6 function and due to the 'Display' Option it will Output strings like this
Iteration Func-count min f(x) Procedure
0 1 36.9193
1 5 35.9815 initial simplex
2 7 35.4924 contract inside
3 9 35.4924 contract inside
4 11 33.0085 expand
So in the command window, i get the function Count for each Iteration step. The structure output, which is created by fminsearch has only the total amount of func-count in it. Is there a way to receive all the Information, that is outputed in the command window also in the Output-structure?
EDIT:
I think i'm pretty Close to the solution. I wrote this outputfunction:
function stop = outfun(x,optimvalues,state);
stop = false;
if state == 'iter'
history = evalin('base','history');
history = [history; optimvalues.iteration optimvalues.funcCount];
assignin('base','history',history);
end
end
due to http://de.mathworks.com/help/matlab/math/output-functions.html this should work, but in fact, matlab tells me,
??? Reference to non-existent field 'funcCount'.
any idea, why this happens?

Performing Kernel Density Estimations in MATLAB

I have been using MATLAB to perform Kernel Density Estimations (KDE) on UTM data (X and Y coordinates). I ran into a problem that I do not seem to be understanding.
I perform the KDEs with a sample of 45 points. Everything works fine and I produce the graphs with contours.
[bandwidth,density,X,Y]=kde2d(data)
The function kde2d is code by Zdravko Botev. I obtained it from his file exchange on MathWorks. The variable 'data' is a 45x2 array of my data. The first column holds the X coordinates and the second the Y.
The problem comes when I try to do the same line of code on a subset of those 45 points. I get a recurring error:
Error using fzero (line 274)
The function values at the interval endpoints must differ in sign.
Error in kde2d (line 101)
t_star=fzero(#(t)(t-evolve(t)),[0,0.1]);
I get the same error for a bunch of those subsets on a bunch of different sets of 45 points.
The complete set has these 45 values:
1594436.281 572258.1272
1594418.48 572357.5859
1594471.362 572385.5186
1594516.726 572266.8206
1594415.313 572369.2754
1594519.701 572272.7153
1594415.377 572363.4139
1594468.365 572381.5779
1594518.139 572276.6059
1594425.496 572271.6874
1594524.259 572272.7651
1594502.555 572172.8749
1594516.747 572264.867
1594485.314 572360.2689
1594476.027 572375.7997
1594556.087 572419.6609
1594522.718 572274.7021
1594472.775 572395.3039
1594554.568 572419.6443
1594527.255 572276.7054
1594474.315 572393.3669
1594522.697 572276.6557
1594471.319 572389.4262
1594460.854 572373.6799
1594546.022 572228.0609
1594460.79 572379.5414
1594468.323 572385.4855
1594466.953 572371.7926
1594519.722 572270.7614
1594396.76 572398.3826
1594468.131 572403.0693
1594418.288 572375.1697
1594396.377 572433.5499
1594448.287 572271.9361
1594510.541 572276.523
1594424.466 572226.7345
1594413.773 572371.2124
1594511.848 572296.0774
1594513.367 572296.094
1594424.488 572224.7805
1594468.152 572401.1153
1594421.37 572371.2953
1594446.768 572271.9195
1594468.152 572401.1153
1594448.799 572225.0457
One of the subsets I am trying to use is this:
1594436.281 572258.1272
1594418.48 572357.5859
1594471.362 572385.5186
1594516.726 572266.8206
1594415.313 572369.2754
1594519.701 572272.7153
1594415.377 572363.4139
1594468.365 572381.5779
1594518.139 572276.6059
1594425.496 572271.6874
I am not sure if I should include any of Botev's code. I am hoping that the error message can be explained on its own. If not I can provide more. Thank you very much.

Catching a keyboard keypress in a MATLAB GUI

I am using matlab and I want to have radio buttons, with some keys mapped to it.
The following code works for displaying radio button and taking values from the user. Can someone help me map keys 1 for Female and 2 for Male.
hgen_radio = uibuttongroup('visible','on','Units','pixels','Position',[1750 1045 170,50],'Title','Gender');
set(hgen_radio,'SelectionChangeFcn',#isMale_Callback);
ugen0 = uicontrol('Style','Radio','String','Male',...
'pos',[10 5 50 25],'parent',hgen_radio,'HandleVisibility','off');
ugen1 = uicontrol('Style','Radio','String','Female',...
'pos',[70 5 70 25],'parent',hgen_radio,'HandleVisibility','off');
set(hgen_radio,'SelectedObject',[]);
Thanks in advance!
In MATLAB, keypresses have to be caught by the figure containing the UICONTROLs, and then passed through. Here's one easy way to do this.
hgen_radio = uibuttongroup('visible','on','Units','Normalized','Position',[.2 .2 .2 .2],'Title','Gender');
set(hgen_radio,'SelectionChangeFcn',#(x,y)disp('Clicked!'));
ugen0 = uicontrol('Style','Radio','String','Male',...
'pos',[10 5 50 25],'parent',hgen_radio,'HandleVisibility','off');
ugen1 = uicontrol('Style','Radio','String','Female',...
'pos',[70 5 70 25],'parent',hgen_radio,'HandleVisibility','off');
set(hgen_radio,'SelectedObject',[]);
set(gcf,'keypressFcn',#(x,y)catchKey(hgen_radio,x,y))
where catchKey.m has something like:
function catchKey(hgen_radio,varargin)
switch varargin{2}.Key
case {'1','2'}
%to do: check if previous value was female, or male...
feval(get(hgen_radio,'SelectionChangeFcn'))
otherwise
%pass other keypresss through
end
See: http://www.mathworks.com/matlabcentral/answers/1450 for another example.
For applications of any complexity, you shouldn't pass handles around like I did above; instead store the values of all the handles in APPDATA using setappdata and getappdata. This significantly clarifies a lot of code. e.g. http://www.mathworks.com/help/matlab/ref/setappdata.html