如何使用命令将.fig格式绘图的属性复制到另一绘图?
If you need to replicate line widths, axes settings, all font properties, background colors, figure size/position from one saved .fig file to another, here's a practical, step-by-step approach using MATLAB's handle graphics tools:
Step 1: Open Both Figures and Grab Their Handles
First, load your reference (source) figure and the target figure you want to update, then capture their handles:
% Open the source figure with the properties you want to copy fig_source = open('your_source_figure.fig'); % Open the target figure that needs the property update fig_target = open('your_target_figure.fig');
Step 2: Copy Line Properties (Including Line Width)
Lines are child objects of the axes. We’ll loop through each line in the source axes and mirror its properties to the corresponding line in the target:
% Get axes handles (works for single-axis figures; loop for subplots) ax_source = fig_source.Children; ax_target = fig_target.Children; % Retrieve all line objects from both axes lines_source = ax_source.Children; lines_target = ax_target.Children; % Copy line properties (skip read-only attributes like parent/type) for i = 1:length(lines_source) line_attrs = get(lines_source(i)); line_attrs = rmfield(line_attrs, {'Type', 'Parent', 'Annotation', 'DisplayName'}); set(lines_target(i), line_attrs); end
This covers line width, color, style, marker type, and other line-specific properties.
Step 3: Copy Axes Settings (Fonts, Ticks, Background)
Next, transfer all axes-level properties—this includes axis limits, tick labels, axes background color, and base font configurations:
% Get source axes properties, exclude unmodifiable/structural attributes ax_attrs = get(ax_source); ax_attrs = rmfield(ax_attrs, {'Type', 'Parent', 'Children', 'Position', 'Units'}); % Apply properties to the target axes set(ax_target, ax_attrs);
Step 4: Enforce Full Font Consistency
To ensure every text element (axis labels, title, tick labels) matches exactly, explicitly copy font properties:
% Extract font details from the source axes font_attrs = get(ax_source, {'FontName', 'FontSize', 'FontWeight', 'FontAngle'}); % Apply to all text components set(ax_target.XLabel, font_attrs); set(ax_target.YLabel, font_attrs); set(ax_target.Title, font_attrs); set(ax_target, {'FontName', 'FontSize', 'FontWeight', 'FontAngle'}, font_attrs);
Step 5: Copy Figure Size, Position, and Background Color
Finally, replicate the figure’s window dimensions, on-screen position, and background color:
% Get figure position, color, and unit settings fig_attrs = get(fig_source, {'Position', 'Color', 'Units'}); % Set units first to ensure position is applied correctly set(fig_target, 'Units', fig_attrs{3}); % Apply position and background color set(fig_target, 'Position', fig_attrs{1}); set(fig_target, 'Color', fig_attrs{2});
Quick Edge Case Tips
- For figures with multiple subplots, wrap the axes/line copying logic in a loop over each axes object in
fig_source.Children. - If source and target have different line counts, adjust the loop to handle mismatches (e.g., copy properties to the first N lines, or add new lines if needed).
- Test with a small subset first to confirm properties are copying as expected before running the full script.
内容的提问来源于stack exchange,提问作者John Smith

