📄 guistruct.m
字号:
% check for valid scalar fields
% truncate vectors for scalar fields
if (~isnumeric(element_list(i).value) || isempty(element_list(i).value))
element_flag(i) = 0;
else
element_list(i).value = element_list(i).value(1);
end
case {'vector', 'fixed'}
% check for valid vector fields
% add space to vector field format for integer spacing
if (~isnumeric(element_list(i).value) || isempty(element_list(i).value))
element_flag(i) = 0;
elseif (all(round(element_list(i).value) == element_list(i).value))
element_list(i).format = [element_list(i).format ' '];
end
case 'enumerated'
% make sure 'enumerated' fields have cell array format for list
% make sure 'enumerated' values are strings
% add enumerated value if not already in list
if (~iscellstr(element_list(i).format))
element_flag(i) = 0;
err_msg = sprintf('%s - format field is not cell array of strings', err_msg);
elseif (~ischar(element_list(i).value) || isempty(element_list(i).value))
element_flag(i) = 0;
elseif (isempty(strmatch(element_list(i).value, element_list(i).format, 'exact')))
element_list(i).format{end + 1} = element_list(i).value;
end
case 'string'
% check for valid vector fields
if (~ischar(element_list(i).value))
element_flag(i) = 0;
end
otherwise
% invalid MULTI flag
element_flag(i) = 0;
end
% display error message if field ignored
if (~element_flag(i))
warning('MATLAB:guistruct', err_msg);
end
end
% make sure something passed through to be edited
element_index = find(element_flag);
if (isempty(element_index))
warning('MATLAB:guistruct', 'all values have been ignored, exiting ...')
for i = 1 : nargout
switch (i)
case 1
varargout{i} = edit_struct;
case 2
varargout{i} = 0;
otherwise
varargout{i} = [];
end
end
return
else
% MOD 6/4/04 SCM
% account for optional sort field
[element_sort, element_index] = sort(element_sort(element_index));
end
% determine layout of edit boxes on figure window
% calculate size & location of figure window
% things will be tight if more than 60 elements!
edit_col = ceil(length(element_index) / 20);
edit_row = ceil(length(element_index) / edit_col);
fig_height = min(0.80, (0.10 + 0.035 * edit_row));
fig_width = min(0.80, (0.05 + 0.250 * edit_col));
fig_pos = [(1 - fig_width)/2 (1 - fig_height)/2 fig_width fig_height];
% calculate size & spacing of edit boxes
% text label width = edit box width
% horizontal space = edit box width / 4
% vertical space = edit box height / 2
edit_width = 4 / (9 * edit_col + 1);
space_width = edit_width / 4;
edit_height = 2 / (3 * edit_row + 6);
space_height = edit_height / 2;
% calculate size & spacing of OK & Cancel buttons
button_height = 3 * edit_height / 2;
button_width = min(0.4, 3 * edit_width / 2);
button_left = (1 - 2 * button_width) / 3;
button_right = 2 * button_left + button_width;
% create a new figure window to hold the edit boxes
h_fig = figure('Units', 'normalized', ...
'Position', fig_pos, ...
'MenuBar', 'none', ...
'Name', fig_title, ...
'NumberTitle', 'off', ...
'Resize', 'off', ...
'Tag', '', ...
'UserData', [], ...
'CloseRequestFcn', 'set(gcf, ''Tag'', ''cancel'')');
% create array of edit boxes and text labels
% loop through array elements to create edit boxes
x_pos = space_width;
y_pos = 1;
enable_string = {'off', 'on'};
for i = 1 : length(element_index)
y_pos = y_pos - edit_height - space_height;
h_text(i) = uicontrol('Parent', h_fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'Position', [x_pos y_pos edit_width edit_height], ...
'BackgroundColor', get(h_fig, 'Color'), ...
'ForegroundColor', [0 0 0], ...
'FontName', 'helvetica', ...
'FontSize', 8, ...
'HorizontalAlignment', 'right', ...
'String', sprintf('%s : ', element_list(element_index(i)).title));
% create edit box or drop-down list depending on MULTI flag
% MOD 6/4/04 SCM
% account for optional enable field
h_edit(i) = uicontrol('Parent', h_fig, ...
'Units', 'normalized', ...
'Position', [(x_pos + edit_width) y_pos edit_width edit_height], ...
'BackgroundColor', [1 1 1], ...
'ForegroundColor', [0 0 0], ...
'Enable', enable_string{element_enable(element_index(i)) + 1}, ...
'FontName', 'helvetica', ...
'FontSize', 8, ...
'UserData', element_list(element_index(i)), ...
'Tag', sprintf('%d', element_index(i)));
if (strcmp(element_list(element_index(i)).multi, 'enumerated'))
set(h_edit(i), 'HorizontalAlignment', 'left', ...
'Style', 'popupmenu',...
'String', element_list(element_index(i)).format, ...
'Value', strmatch(element_list(element_index(i)).value, element_list(element_index(i)).format, 'exact'), ...
'Callback', '');
else
edit_string = rmblank(sprintf(element_list(element_index(i)).format, element_list(element_index(i)).value));
% allow for multiline text if needed
if (isempty(find(edit_string == sprintf('\n'))))
max_value = 1;
else
max_value = 2;
end
set(h_edit(i), 'HorizontalAlignment', 'center', ...
'Style', 'edit', ...
'Min', 0, 'Max', max_value, ...
'String', rmblank(sprintf(element_list(element_index(i)).format, element_list(element_index(i)).value)), ...
'Callback', 'guistruct(gcbo)');
end
% check for next column
if (mod(i, edit_row) == 0)
x_pos = x_pos + 2 * edit_width + space_width;
y_pos = 1;
end
end
% create 'OK' & 'Cancel' buttons
% create separator
y_pos = space_height;
h_ok = uicontrol('Parent', h_fig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [button_left y_pos button_width button_height], ...
'Callback', 'set(gcf, ''Tag'', ''ok'')', ...
'String', 'OK');
h_cancel = uicontrol('Parent', h_fig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [button_right y_pos button_width button_height], ...
'Callback', 'set(gcf, ''Tag'', ''cancel'')', ...
'String', 'Cancel');
y_pos = y_pos + button_height + space_height;
h_frame = uicontrol('Parent', h_fig, ...
'Style', 'frame', ...
'Units', 'normalized', ...
'Position', [0 y_pos 1 edit_height/20]);
% wait for user to click button or close edit figure
% copy structure values if selected
waitfor(h_fig, 'Tag');
edit_flag = strcmp(get(h_fig, 'Tag'), 'ok');
if (edit_flag)
for i = 1 : length(h_edit)
% update structure values from edit box UserData
% store string for enumerated values
% validation routine handles string conversions
element_index = str2num(get(h_edit(i), 'Tag'));
element_struct = get(h_edit(i), 'UserData');
if (strcmp(element_struct.multi, 'enumerated'))
element_struct.value = element_struct.format{get(h_edit(i), 'Value')};
end
% copy element values to appropriate structure type
switch (struct_type)
case 'regular'
edit_struct.(element_struct.name) = element_struct.value;
case 'native'
edit_struct(element_index).value = element_struct.value;
case 'PBM'
pbm_struct = edit_struct.(element_struct.name);
pbm_struct.v = element_struct.value;
edit_struct.(element_struct.name) = pbm_struct;
end
end
end
% delete edit figure
if (istype(h_fig, 'figure'))
delete(h_fig);
end
% return outputs if specified
for i = 1 : nargout
switch (i)
case 1
varargout{i} = edit_struct;
case 2
varargout{i} = edit_flag;
otherwise
varargout{i} = [];
end
end
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -