将 GUIDE GUI 小部件升级到最新的 Matlab 版本
Upgrade GUIDE GUI widgets to latest Matlab version
我有一些在 Matlab 2010b 下使用 GUIDE 创建的 GUI。将 Matlab 升级到 2015b 后,我看到一些小部件现在具有不同的设计,并且我的旧 GUI 的外观不匹配。有什么方法可以升级 GUI 使其具有与 2015b 兼容的外观吗?这是显示不匹配的小部件的屏幕截图。
我看到了一些可以为您执行此操作的升级脚本的参考资料,但我在官方 matlab 文档中没有看到任何参考资料。
MATLAB 没有任何官方方法可以做到这一点。您看到的这种差异是由于版本之间的默认 uicontrol
和 uipanel
BackgroundColor
属性 不同所致。我在下面有一个脚本,它实际上可以加载到 .fig
文件(使用 GUIDE 或其他方式创建)并用当前默认值替换 uicontrol
或 uipanel
对象的 BackgroundColors
背景颜色。然后重新保存 .fig
文件,同时保留原始文件的备份。
function varargout = updatebgcolor(figfile)
% updatebgcolor - Updates the uicontrol background colors
%
% USAGE:
% updatebgcolor(figfile)
data = load(figfile, '-mat');
% Types of controls to update
types = {'uicontrol', 'uipanel'};
% Get the current default background color
bgcolor = get(0, 'DefaultUIControlBackgroundColor');
% Switch out all of the background colors
data2 = updateBackgroundColor(data, types, bgcolor);
% Resave the .fig file at the original location
movefile(figfile, [figfile, '.bkup']);
save(figfile, '-struct', 'data2')
if nargout; varargout = {data2}; end
end
function S = updateBackgroundColor(S, types, bgcolor)
% If this is not a struct, ignore it
if ~isstruct(S); return; end
% Handle when we have an array of structures
% (call this function on each one)
if numel(S) > 1
S = arrayfun(@(s)updateBackgroundColor(s, types, bgcolor), S);
return
end
% If this is a type we want to check and it has a backgroundcolor
% specified, then update the stored value
if isfield(S, 'type') && isfield(S, 'BackgroundColor') && ...
ismember(S.type, types)
S.BackgroundColor = bgcolor;
end
% Process all other fields of the structure recursively
fields = fieldnames(S);
for k = 1:numel(fields)
S.(fields{k}) = updateBackgroundColor(S.(fields{k}), types, bgcolor);
end
end
我有一些在 Matlab 2010b 下使用 GUIDE 创建的 GUI。将 Matlab 升级到 2015b 后,我看到一些小部件现在具有不同的设计,并且我的旧 GUI 的外观不匹配。有什么方法可以升级 GUI 使其具有与 2015b 兼容的外观吗?这是显示不匹配的小部件的屏幕截图。
我看到了一些可以为您执行此操作的升级脚本的参考资料,但我在官方 matlab 文档中没有看到任何参考资料。
MATLAB 没有任何官方方法可以做到这一点。您看到的这种差异是由于版本之间的默认 uicontrol
和 uipanel
BackgroundColor
属性 不同所致。我在下面有一个脚本,它实际上可以加载到 .fig
文件(使用 GUIDE 或其他方式创建)并用当前默认值替换 uicontrol
或 uipanel
对象的 BackgroundColors
背景颜色。然后重新保存 .fig
文件,同时保留原始文件的备份。
function varargout = updatebgcolor(figfile)
% updatebgcolor - Updates the uicontrol background colors
%
% USAGE:
% updatebgcolor(figfile)
data = load(figfile, '-mat');
% Types of controls to update
types = {'uicontrol', 'uipanel'};
% Get the current default background color
bgcolor = get(0, 'DefaultUIControlBackgroundColor');
% Switch out all of the background colors
data2 = updateBackgroundColor(data, types, bgcolor);
% Resave the .fig file at the original location
movefile(figfile, [figfile, '.bkup']);
save(figfile, '-struct', 'data2')
if nargout; varargout = {data2}; end
end
function S = updateBackgroundColor(S, types, bgcolor)
% If this is not a struct, ignore it
if ~isstruct(S); return; end
% Handle when we have an array of structures
% (call this function on each one)
if numel(S) > 1
S = arrayfun(@(s)updateBackgroundColor(s, types, bgcolor), S);
return
end
% If this is a type we want to check and it has a backgroundcolor
% specified, then update the stored value
if isfield(S, 'type') && isfield(S, 'BackgroundColor') && ...
ismember(S.type, types)
S.BackgroundColor = bgcolor;
end
% Process all other fields of the structure recursively
fields = fieldnames(S);
for k = 1:numel(fields)
S.(fields{k}) = updateBackgroundColor(S.(fields{k}), types, bgcolor);
end
end