?? exportga2wsdlg.m
字號:
function exportga2wsdlg(probmodel, optmodel, randchoice)
%EXGAPORT2WSDLG Exports variables to the workspace.
% Copyright 2004 The MathWorks, Inc.
% $Revision: 1.18.4.2 $ $Date: 2004/03/18 17:59:28 $
title = 'Export To Workspace';
hDialog = dialog('Visible', 'off', 'Name', title, 'WindowStyle', 'normal');
defaultVariableNames = {'gaproblem'; ' '; 'gaoptions'; 'garesults'};
variableNames = createVarNames(defaultVariableNames);
cancelButton = uicontrol(hDialog,'String', 'Cancel',...
'Callback', {@CancelCallback, hDialog});
okButton = uicontrol(hDialog,'String', 'OK', 'Fontweight', 'bold');
checkboxLabels = {'Export problem and options to a MATLAB structure named:'; ...
'Include information needed to resume this run';...
'Export options to a MATLAB structure named:';...
'Export results to a MATLAB structure named:'};
%Retrieve problem
[fitnessFcn,nvars,randstate,randnstate] = gaguiReadProblem(probmodel);
%Retrieve results
gaResults = getappdata(0,'gads_gatool_results_121677');
if ~isempty(gaResults)
x = gaResults.x;
fval = gaResults.fval;
output = gaResults.output;
exitMessage = output.message;
pop = gaResults.population;
scores = gaResults.scores;
else
x = []; fval = []; output = [];
exitMessage = []; pop = []; scores = [];
end
if isempty(x)
disableFields = true;
else
disableFields = false;
end
[checkBoxes, editFields] = layoutDialog(hDialog, okButton, cancelButton, ...
checkboxLabels, variableNames, fitnessFcn,nvars,disableFields);
set(okButton, 'Callback', {@OKCallback, hDialog, checkBoxes, editFields, ...
optmodel, x, fval, exitMessage, output,fitnessFcn, ...
nvars,pop, scores, randstate, randnstate, randchoice});
set(hDialog, 'KeyPressFcn', {@KeyPressCallback, hDialog, checkBoxes, editFields, ...
optmodel, x, fval, exitMessage, output,fitnessFcn, ...
nvars,pop, scores, randstate, randnstate, randchoice});
%set(hDialog, 'HandleVisibility', 'callback', 'WindowStyle', 'modal');
set(hDialog, 'Visible', 'on');
%----------------------------------------------------------------------------
function modifiedNames = createVarNames(defVariableNames)
% Preallocating for speed
modifiedNames = cell(1, length(defVariableNames));
for i = 1:length(defVariableNames)
modifiedNames{i} = computename(defVariableNames{i});
end
%----------------------------------------------------------------------------
function name = computename(nameprefix)
if (evalin('base',['exist(''', nameprefix,''', ''var'');']) == 0)
name = nameprefix;
return
end
% get all names that start with prefix in workspace
workvars = evalin('base', ['char(who(''',nameprefix,'*''))']);
% trim off prefix name
workvars = workvars(:,length(nameprefix)+1:end);
if ~isempty(workvars)
% remove all names with suffixes that are "non-numeric"
lessthanzero = workvars < '0';
morethannine = workvars > '9';
notblank = (workvars ~= ' ');
notnumrows = any((notblank & (lessthanzero | morethannine)),2);
workvars(notnumrows,:) = [];
end
% find the "next one"
if isempty(workvars)
name = [nameprefix, '1'];
else
nextone = max(str2num(workvars)) + 1;
if isempty(nextone)
name = [nameprefix, '1'];
else
name = [nameprefix, num2str(nextone)];
end
end
%----------------------------------------------------------------------------
function OKCallback(obj, eventdata, dialog, cb, e, model, x, fval, exitMessage,output, ...
fitnessFcn,nvars,pop, scores, randstate, randnstate, randchoice)
CB_PROBLEM = 1;
CB_RESTART = 2;
CB_OPTION = 3;
CB_RESULTS = 4;
varnames = [];
% we care only about items that are checked
for i = 1:length(e)
if get(cb{i}, 'Value') == 1 && i~=CB_RESTART
varnames{end + 1} = get(e{i}, 'String');
end
end
if isempty(varnames)
errordlg('You must select an item to export', ...
'Export to Workspace');
return;
end
%check for invalid and empty variable names
badnames = [];
numbadentries = 0;
emptystrmsg = '';
badnamemsg = '';
for i = 1:length(varnames)
if strcmp('', varnames{i})
numbadentries = numbadentries + 1;
emptystrmsg = sprintf('%s\n', ...
'An empty string is not a valid choice for a variable name.');
elseif ~isvarname(varnames{i})
badnames{end + 1} = varnames{i};
numbadentries = numbadentries + 1;
end
end
badnames = unique(badnames);
if ~isempty(badnames)
if (length(badnames) == 1)
badnamemsg = ['"' badnames{1} '"' ...
' is not a valid MATLAB variable name.'];
elseif (length(badnames) == 2)
badnamemsg = ['"' badnames{1} '" and "' badnames{2} ...
'" are not valid MATLAB variable names.'];
else
badnamemsg = [sprintf('"%s", ', badnames{1:end-2}),...
'"' badnames{end-1} ...
'" and "' badnames{end} ...
'" are not valid MATLAB variable names.', ];
end
end
if numbadentries > 0
dialogname = 'Invalid variable names';
if numbadentries == 1
dialogname = 'Invalid variable name';
end
errordlg([emptystrmsg badnamemsg], dialogname);
return;
end
%check for names already in the workspace
dupnames = [];
for i = 1:length(varnames)
if evalin('base',['exist(''',varnames{i},''', ''var'');'])
dupnames{end + 1} = varnames{i};
end
end
dupnames = unique(dupnames);
if ~isempty(dupnames)
dialogname = 'Duplicate variable names';
if (length(dupnames) == 1)
queststr = ['"' dupnames{1} '"'...
' already exists. Do you want to overwrite it?'];
dialogname = 'Duplicate variable name';
elseif (length(dupnames) == 2)
queststr = ['"' dupnames{1} '" and "' dupnames{2} ...
'" already exist. Do you want to overwrite them?'];
else
queststr = [sprintf('"%s" , ', dupnames{1:end-2}), ...
'"' dupnames{end-1} '" and "' dupnames{end} ...
'" already exist. Do you want to overwrite them?'];
end
buttonName = questdlg(queststr, dialogname, 'Yes', 'No', 'Yes');
if ~strcmp(buttonName, 'Yes')
return;
end
end
%Check for variable names repeated in the dialog edit fields
[uniqueArray ignore uniqueIndex] = unique(varnames);
if length(varnames) == length(uniqueArray)
if get(cb{CB_PROBLEM}, 'Value') == 1
tempstruct = struct;
tempstruct.fitnessfcn=fitnessFcn;
tempstruct.nvars=nvars;
if randchoice
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -