如何在 MATLAB 中跨函数调用刷新注释和子图

发布于 2024-12-05 00:36:28 字数 507 浏览 1 评论 0原文

我正在使用 MATLAB 进行数据分析。在我的脚本中,我创建带有拟合结果的图形,以便我可以快速使用拟合参数并查看它们如何改变我的最终结果。

我的问题是是否有一种简单的方法能够刷新我的图形以及子图和注释不会丢失子图和注释的位置和大小。 即:我希望能够在工作区上手动定位图形(我使用 Linux),手动调整图形大小/位置、子图大小/位置和注释大小/位置,然后更新其内容,当我重新运行执行我的拟合的脚本时。

我确实意识到命令图(...)做得很好并且有效,但是我遇到了问题,当我调整大小/移动子图并移动注释时,当我重新运行时,它们的大小/位置会丢失脚本。

我知道我可能需要使用子图/注释句柄,但问题是,最优雅和最简单的方法是什么?由于我需要代码在第一次运行时也能工作(即尚不存在图形/子图/注释),我是否需要大量 if 子句来检查句柄是否已经存在?

我已经使用 MATLAB 相当长一段时间了,并且几乎同样长的时间里,我不知道一种优雅的方法来做到这一点,这一直困扰着我!

I am using MATLAB for my data analysis. In my scripts I create figures with fit results so that I can quickly play with fit parameters and see how they change my end results.

My question is whether there is a simple way to be able to just refresh my figures along with subplots and annotations without losing the positions and sizes of the subplots and annotations.
I.e.: I would like to be able manually position my figures on my workspace (I use Linux), manually adjust figure size/position, subplot sizes/positions and annotation sizes/positions and then have their content update, when I rerun the script that does my fitting.

I do realize that the command figure(...) does this nicely and it works, but I am having the problem, that when I resize/move subplots and move annotations, that they're sizes/positions get lost, when I rerun the script.

I am aware that I probably need to use the subplot/annotation handles for this but the question is, what the most elegant and simple way is to do this? Since I need the code to also work when run the first time (i.e. no figures/subplot/annoations yet existent), will I need a lot of if-clause, to check if the handles already exist?

I've been using MATLAB for quite some time and for almost equally long it's been bothering me that I don't know an elegant way to do this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-12-12 00:36:28

我有两个想法:

  1. 使用“文件>生成代码...”功能。 MATLAB 将创建
    通过您所做的任何修改重新创建图形的函数
    交互方式。

  2. 手动检索所操作对象的感兴趣属性
    并在重新运行脚本时再次应用它们。你可以
    维护这些图形对象的句柄列表,甚至使用
    'Tag' 与 FINDOBJ 函数结合来定位此类对象。

我将用一个例子来说明后一个想法:

当第一次运行脚本时,我们让用户有机会以交互方式更改图形。完成后,我们检索图形及其中包含的所有子组件的 'Position' 属性。然后将这些值保存到 MAT 文件中。

现在用户调整一些参数并重新运行脚本。我们检查 MAT 文件是否存在。如果存在,我们加载保存的位置值并将它们应用于图形及其后代对象,从而将组件恢复到上次保存的状态。

此解决方案相当简单,因此,如果对脚本进行的更改破坏了图形句柄的层次结构,则必须删除 MAT 文件,然后再次运行脚本。

%# close all figures
close all

%# your script which creates figures
figure, plot(rand(100,1))
figure
subplot(121), plot( cos(linspace(0,6*pi,100)) )
subplot(122), image, axis image, axis ij

%# check for MAT-file
if exist('script_prefs.mat','file')
    %# load saved values
    load script_prefs.mat

    %# get opened figures, and find objects with a position property
    fig = get(0, 'Children');          %# allchild(0)
    obj = findobj(fig, '-property','position');

    try
        %# apply values to position property
        set(fig, {'Position'},figPos);
        set(obj, {'Position'},objPos);
    catch
        %# delete MAT-file
        delete script_prefs.mat
    end
else
    %# get opened figures, and find objects with a position property
    fig = get(0, 'Children');
    obj = findobj(fig, '-property','position');

    %# wait for the user to finish customizing
    waitFig = figure('Menubar','none', 'Position',[200 200 200 40]);
    h = uicontrol('Units','normalized', 'Position',[0 0 1 1], ...
        'String','Done?', 'Callback','uiresume(gcbf)');
    uiwait(waitFig); 
    close(waitFig);

    %# get position property of figures and tagged objects
    figPos = get(fig, 'Position');
    objPos = get(obj, 'Position');

    %# save values to file
    save script_prefs.mat figPos objPos
end

I had two ideas:

  1. use the "File > Generate Code..." functionality. MATLAB will create
    a function that recreates the figure with any modification you made
    interactively.

  2. manually retrieve the properties of interest for the objects manipulated
    and apply them again when you rerun your scripts. You could either
    maintain a list of handles for those graphics objects, or even use the
    'Tag' in combination with the FINDOBJ function to locate such objects.

I will illustrate the latter idea with an example:

When the script is run for the first time, we give the user the chance to make changes to the figures interactively. Once done, we retrieve the 'Position' property of figures and all children components contained inside them. These values are then saved to a MAT-file.

Now the user adjusts some parameters and reruns the script. We check for the presence of the MAT-file. If it exists, we load the saved position values and apply them to the figures and their descendant objects, thus restoring the components to their last saved state.

This solution is rather simplistic, thus if changes are made to the script breaking the hierarchy of the graphics handles, you will have to delete the MAT-file, and run the script again.

%# close all figures
close all

%# your script which creates figures
figure, plot(rand(100,1))
figure
subplot(121), plot( cos(linspace(0,6*pi,100)) )
subplot(122), image, axis image, axis ij

%# check for MAT-file
if exist('script_prefs.mat','file')
    %# load saved values
    load script_prefs.mat

    %# get opened figures, and find objects with a position property
    fig = get(0, 'Children');          %# allchild(0)
    obj = findobj(fig, '-property','position');

    try
        %# apply values to position property
        set(fig, {'Position'},figPos);
        set(obj, {'Position'},objPos);
    catch
        %# delete MAT-file
        delete script_prefs.mat
    end
else
    %# get opened figures, and find objects with a position property
    fig = get(0, 'Children');
    obj = findobj(fig, '-property','position');

    %# wait for the user to finish customizing
    waitFig = figure('Menubar','none', 'Position',[200 200 200 40]);
    h = uicontrol('Units','normalized', 'Position',[0 0 1 1], ...
        'String','Done?', 'Callback','uiresume(gcbf)');
    uiwait(waitFig); 
    close(waitFig);

    %# get position property of figures and tagged objects
    figPos = get(fig, 'Position');
    objPos = get(obj, 'Position');

    %# save values to file
    save script_prefs.mat figPos objPos
end
假装爱人 2024-12-12 00:36:28

我认为你的意思是你想刷新情节本身,而不是其他任何东西。

当您执行 plot() 时,指定一个输出参数来检索线句柄。然后,当您想要绘制不同的数据时,请手动调整该线句柄的 XDataYData

lh = plot(xdata,ydata);

%# do some calculations here
...

%# calculated new values: newX and newY
set(lh, 'XData', newx, 'YData', newy);

这同样适用于您想要刷新但不重新创建的任何其他内容 - 获取图形对象对应的句柄并在低级别手动更新其属性。

I take it you mean you want to refresh the plots themselves, but not anything else.

When you perform a plot(), specify an output argument to retrieve a line handle. Then, when you want to plot different, data, manually adjust that line handles' XData and YData:

lh = plot(xdata,ydata);

%# do some calculations here
...

%# calculated new values: newX and newY
set(lh, 'XData', newx, 'YData', newy);

This is likewise for anything else you want to refresh but not recreate - get the handle corresponding to the graphics object and manually update its properties at a low level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文