📄 exportps2wsdlg.m
字号:
function exportps2wsdlg(probmodel, optmodel, randchoice)
%EXPSPORT2WSDLG Exports variables to the workspace.
% Copyright 2004 The MathWorks, Inc.
% $Revision: 1.19.4.2 $ $Date: 2004/03/18 17:59:29 $
title = 'Export To Workspace';
hDialog = dialog('Visible', 'off', 'Name', title, 'WindowStyle', 'normal');
defaultVariableNames = {'psproblem';
' ';
'psoptions';
'psresults'};
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
[objFunction, startPoint,aInEq,bInEq,aEq,bEq,lBnds,uBnds,randstate, randnstate] = psguiReadProblem(probmodel);
%Retrieve results
psResults = getappdata(0,'gads_psearchtool_results_033051');
if ~isempty(psResults)
x = psResults.x;
fval = psResults.fval;
output = psResults.output;
exitMessage = output.message;
else
x = []; fval =[]; output = []; exitMessage = [];
end
if isempty(x)
disableFields = true;
else
disableFields = false;
end
[checkBoxes, editFields] = layoutDialog(hDialog, okButton, cancelButton, ...
checkboxLabels, variableNames, disableFields, objFunction, startPoint);
set(okButton, 'Callback', {@OKCallback, hDialog, checkBoxes, editFields, ...
optmodel, x, fval, exitMessage,output, ...
objFunction, startPoint, lBnds, uBnds, aInEq, ...
bInEq, aEq, bEq, randstate, randnstate, randchoice});
set(hDialog, 'KeyPressFcn', {@KeyPressCallback, hDialog, checkBoxes, editFields, ...
optmodel, x, fval, exitMessage,output, ...
objFunction, startPoint, lBnds, uBnds, aInEq, ...
bInEq, aEq, bEq, 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, ...
objFunction, startPoint, lBnds, uBnds, aInEq, bInEq, aEq, bEq, ...
randstate, randnstate, randchoice)
CB_PROBLEM = 1;
CB_OPTION = 3;
CB_RESTART = 2;
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 = [];
emptystrmsg = '';
badnamemsg = '';
numbadentries = 0;
for i = 1:length(varnames)
if strcmp('', varnames{i})
emptystrmsg = sprintf('%s\n', ...
'An empty string is not a valid choice for a variable name.');
numbadentries = numbadentries + 1;
elseif ~isvarname(varnames{i})
badnames{end + 1} = varnames{i};
numbadentries = numbadentries + 1;
end
end
badnames = unique(badnames);
if (length(badnames) > 0)
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 (length(dupnames) > 0)
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
options = psguiReadHashTable(model);
tempstruct = struct;
tempstruct.objective=objFunction;
tempstruct.X0=startPoint;
tempstruct.LB = lBnds;
tempstruct.UB = uBnds;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -