【问题标题】:MATLAB - Plot appearing in new figure?MATLAB - 情节出现在新图中?
【发布时间】:2020-10-31 19:09:44
【问题描述】:

我正在尝试在我已经创建的图形中绘制一个 3D 图形(冲浪图),但由于某种原因,我的新图形是在一个单独的图形中创建的。 该代码由我将发布的 2 个脚本组成。 第一个脚本初始化我想要绘制的动作框架,它的回调(从滑块)绘制我的圆柱体,由于某种原因出现在一个新框架内导致这个混乱:

创建我要绘制的主要图形的主脚本。

%%
clf;
clear;
close all;
clc;
load('myHeatMap.mat','myHeatMap');
filename = ('C:\Users\Ali\Desktop\Documents\DataVis\Projekt\data\day\filenames.txt');
%This line simply gives us a table of all filenames in this file.
T = readtable(filename);
tsize = size(T);
tsize2 = size(T, 1);

filename = strcat('\Users\Ali\Desktop\Documents\DataVis\Projekt\data\day\', string(T{1,1}));
map100 = getCylinderHeatMap(filename);

%Figure/parent container (uifigure) properties%
App = uifigure('Scrollable','on','Name','Heatmap Plots','NumberTitle','off');
App_Width = 1000; App_Height = 500;
App.Position = [0 0 App_Width App_Height];

%Slider label (uilabel) properties%
Slider_Label = uilabel('Parent',App);
Slider_Label.Text = "Cylinder Number";
Slider_Label.Position = [25 20 200 100];

%Slider (uislider) properties%
Slider = uislider('Parent',App);
Slider.Limits = [1 1000];
Slider.Value = 1;
Slider_Width = App_Width - 500;
Margin = (App_Width - Slider_Width)/2;
Slider.Position = [Margin 50 Slider_Width 3];
Slider.MajorTicks = (1:100:1000);
Slider.FontSize = 6;
Red = 87; Green = 207; Blue = 220;
Slider.FontColor = [Red/255 Green/255 Blue/255];

%Plot (uiaxes) properties%
Heatmap_Cylinder_Plot = uiaxes('Parent',App);
Heatmap_Cylinder_Plot_X_Position = 100;
Heatmap_Cylinder_Plot_Y_Position = 100;
Heatmap_Cylinder_Plot_Height = 350;
Heatmap_Cylinder_Plot_Width = 400;
Heatmap_Cylinder_Plot.Position = [Heatmap_Cylinder_Plot_X_Position Heatmap_Cylinder_Plot_Y_Position Heatmap_Cylinder_Plot_Width Heatmap_Cylinder_Plot_Height];
Heatmap_Cylinder_Plot.GridColor = [0.15 0.15 0.15];
Heatmap_Cylinder_Plot.XGrid = 'on';
Heatmap_Cylinder_Plot.YGrid = 'on';
Heatmap_Cylinder_Plot.ZGrid = 'on';

%Image (uiimage) properties%
Heatmap_Image = uiimage('Parent',App);
Heatmap_X_Position = (App_Width/2) + 50;
Heatmap_Y_Position = 80;
Heatmap_Height = 350;
Heatmap_Width = 400;
Heatmap_Image.Position = [Heatmap_X_Position Heatmap_Y_Position Heatmap_Height Heatmap_Width];

%Callback function as the slider is moved%
Slider.ValueChangedFcn = @(Slider,event) Snap_Slider(Slider,Slider_Label,Heatmap_Cylinder_Plot,Heatmap_Image,T,...
    myHeatMap);

%%
%Callback function definition%
function [] = Snap_Slider(Slider,Slider_Label,Heatmap_Cylinder_Plot,Heatmap_Image,T,myHeatMap)
Slider.Value = round(Slider.Value);

filename = strcat('\Users\Ali\Desktop\Documents\DataVis\Projekt\data\day\', string(T{Slider.Value,1}));
map100 = getCylinderHeatMap(filename);
splitFileName = strsplit(string(T{Slider.Value,1}),'.');

Slider_Label.Text = "Time Stamp: " + splitFileName{1,1};

%Put plotting code here%
plot(Heatmap_Cylinder_Plot,createSurfCylinder(map100));

%Put image plotting code here%
Heatmap_Image.ImageSource = "";
colormap(myHeatMap);
end
%%

绘制实际圆柱体的脚本。

function cylinder = createSurfCylinder(matrix) 
%Load heat map.
load('myHeatMap.mat','myHeatMap');

%%
%Cylinder creation
Sample_Range = 255 - 0;
Temperature_Range = 450 - 50;

Multiplier = Temperature_Range/Sample_Range;
map100 = matrix.*Multiplier + 50;


%Setting up the figure%
Radius = 1.5;
Number_Of_Data_Points = 360;
theta = linspace(0,2*pi,Number_Of_Data_Points);

%The xy values according to radius and number of points%
Z_Circle = Radius*cos(theta);
Y_Circle = Radius*sin(theta);

map100 = rot90(map100);

Height = 512;
Z_Circle = repmat(Z_Circle,Height,1);
Y_Circle = repmat(Y_Circle,Height,1);
X_Length = (1:512)';
X_Length = repmat(X_Length,1,Number_Of_Data_Points);

figure('Position', [10 10 800 800])

clf;
close;
cyl = surf(X_Length,Y_Circle,Z_Circle,'Cdata',map100); 
title("3D Cylinder Heatmap Plot");
zlabel("Z-Position");
ylabel("Y-Position");
xlabel("Length(Cm)");
set(gca,'Ydir','reverse')
colormap(myHeatMap);
colorbar;
shading interp

Maximum_Value = 450;
Minimum_Value = 50;
caxis([Minimum_Value Maximum_Value]);

%Show the image in the subplot and add custome color coding to it.
% subplot(1,3,3); imshow(rot90(map100));
% colormap(myHeatMap);
% caxis([Minimum_Value Maximum_Value]);
cylinder = cyl;
%%
end

任何知道如何制作可能圆柱体的人会出现在我原来的 Uiframe 中? 任何帮助将不胜感激。

【问题讨论】:

  • 调用plot() 将始终创建一个新的独立图形,除非指定图形。如果您想在uiaxes 元素上绘图,请使用plot(Heatmap_Cylinder_Plot,[1 2 3 4 5 6 7]);
  • 特别是因为你在圆柱函数中调用了figurefigure 打开一个新的。您需要按照 Michael 的建议传递坐标轴句柄进行绘图

标签: matlab matlab-figure


【解决方案1】:

您可能需要注意一些事项。首先是关闭命令。

  • clf关闭(当前)图
  • clear清除当前工作空间中的所有变量(这可以是脚本中的基础工作空间,也可以是调用命令的函数的工作空间)
  • close all 关闭所有打开的图形窗口
  • clc 清除命令窗口。

虽然这些是方便的命令,但它们是在命令窗口中工作时使用的,在脚本中使用它们时应该三思而后行,切勿在函数中使用它们。为什么这样?您可能会忘记您已经使用过它们,因此您的代码行为会出乎意料。特别是如果您调用多个函数/脚本,它们都操作相同的窗口或输出。

命令figure 打开一个新的图形窗口。因此,每次发出此命令时,都会弹出一个新窗口并设置为“活动”,即自动为当前图形。如果您为其分配输出,它将返回其特定的图形句柄fh = figure('Name','Figure A')。如果你稍后要激活一个特定的图形,你可以调用它的句柄:figure(fh)。函数gcf总是返回当前图形的句柄。

现在,让我们看看,例如,在函数 createSurfCylinder 中,您正在调用

figure('Position', [10 10 800 800])

clf;
close;

这会打开一个图窗,关闭当前图窗(即您刚刚打开的图窗)并关闭当前图窗(现在是您打开并关闭最后一个图窗之前打开的图窗...

plot()-函数在当前中画一条线(如果不存在,它会打开一个轴)。它可以将特定轴句柄作为第一个输入。默认为gca,这是一个函数,含义类似于get current axis

因此,要完全控制您的绘图,请跟踪句柄。我总是这样:

fh1 = struct(); % create an empty struct
fh1.fig = figure('Name','very important figure'); % open figure & keep its handle
fh1.ax = subplot(1,1,1); % this opens a single axis & I keep the handle

% plot
plot(   fh.ax, [1,10],[2,5])
ylabel( fh.ax, 'Amplitude Y / unit')
xlabel( fh.ax, 'Measure of time x / unit')

使用命令subplot可以在一个图形中排列多个轴(这里需要注意figureaxis之间的区别,以及为什么有两种不同类型的手柄)。例如

fh2 = struct(); % create a new empty struct
fh2.fig = figure('Name','Multiple axes in one figure'); % open figure & keep its handle
fh2.ax = cell(3,1);
fh2.ax{1} = subplot(2,2,1); % this opens a single axis & keeps the handle
fh2.ax{2} = subplot(2,2,2); % this opens a single axis & keeps the handle
fh2.ax{3} = subplot(2,2,3:4); % this opens a single axis & keeps the handle

% do plotting with your axis-handles

(是的,我通常先打开所有轴,然后用图形/线条填充它们)

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2019-03-31
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多