(MATLAB)是否可以使用copyfile将从GUI中的轴导入的数据重新绘制到新图形? [英] (MATLAB) Can I re-plot imported data from axes in GUI to new figure using copyfile?

查看:154
本文介绍了(MATLAB)是否可以使用copyfile将从GUI中的轴导入的数据重新绘制到新图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经制作了一个带有按钮的GUI,以选择一个包含文件的文件夹,然后将它们全部绘制在GUI中的单个轴上.

Hello I've made a GUI with a button to select a folder containing files and then plot them all on a single axes in the GUI.

我还创建了一个按钮,以便用户如果要以新图形打开此图,则可以.但是,对于此按钮,他们必须再次选择相同的文件夹(我所做的只是包含图形;轴;使用相同的代码).但是,是否有一种方法可以保存选择的数据,从而使他们不必再次选择文件夹?我正在考虑使用copyfile函数,但我不知道该怎么做...

I've also made a button so that if the user wants to open up this plot in new figure they can. However, for this button, they have to select the same folder again (all I did was include figure; axes; in the same code). But is there a way to save the data that was selected so that they don't have to choose the folder again? I'm thinking of using the copyfile function, but I have no idea how to do it...

这是第一个按钮(导入数据)的代码:

Here is the code for the first button (import data):

d= uigetdir(pwd, 'Select a folder');

files = dir(fullfile(d, '*.txt'));

len = length(files);

linecolors = jet(len);

for i = 1:len

a = files(i).name;

filename{i} = a;

path = [d,'\',a];

data = dlmread(path);

plot(data(:,1), data(:,2),'color',linecolors(i,:),'linewidth',2);

hold on;

end

hold off;

xlabel('Intensity','fontweight','bold');

ylabel('Wave Number','fontweight','bold');

title('Spectra Plot','fontweight','bold','fontsize',14);

legend(filename,'Interpreter','none', 'location', 'southoutside');

这是第二个按钮(在新图中打开)的代码,仅增加了2行(第5行和第6行):

Here is the code for the second button (open in new figure) with just 2 additional lines (5th and 6th line) :

d= uigetdir(pwd, 'Select a folder');

files = dir(fullfile(d, '*.txt'));

len = length(files);

linecolors = jet(len);

figure();

axes;

for i = 1:len

a = files(i).name;

filename{i} = a;

path = [d,'\',a];

data = dlmread(path);

plot(data(:,1), data(:,2),'color',linecolors(i,:),'linewidth',2);

hold on;

end

hold off;  

xlabel('Intensity','fontweight','bold');

ylabel('Wave Number','fontweight','bold');

title('Spectra Plot','fontweight','bold','fontsize',14);

legend(filename,'Interpreter','none', 'location', 'southoutside');

有人可以帮忙吗?

谢谢

维拉

推荐答案

现在,我阅读了您对其他答案的评论,这是一种执行所需操作的更快方法.第一个答案说明了如何从gui的不同部分保存和检索值,但是您需要做的就是在新图中复制轴.

now I read your comment on the other answer, there is a faster way to do what you want. The first answer explained how to save and retrieve values from different part of your gui, but all you need in your case is to do a copy of your axes in a new figure.

另一种实现方法是: 保持按钮1完全相同的代码(我的代码,甚至是您的原始代码),然后保持按钮2完全相同:

So another way to achieve that is : Keep exactly the same code for button 1 (either my code or even your original code), then for button 2:

hfig = ancestor(gcbo,'figure') ;        %// find the handle of the current figure
hax = findobj( hfig , 'type', 'axes') ; %// find the handle of the axes inside the main figure
hfig2 = figure ;                        %// create a new figure
hax2  = copyobj(hax,hfig2);             %// make a full copy of the axes in the new figure

通过这种方式,您不必重新读取所有文件并加载数据.

This way you do not have to re-read all your files and load the data.

初始答案:

您可以使用 setappdata getappdata 命令.

You can save parameters or variables in the figure application data, using the setappdata and getappdata commands.

在这种情况下,您可以保存文件列表,因此您无需再次检索它(假设您将始终首先使用按钮1).

In your case you can save the file list so you do not need to retrieve it again (this assume you will always use the button 1 first).

可以将实际绘制的函数分开,因此您无需重复代码.只需使用目标axes的句柄(地址)作为参数即可.

The function which actually plot can be separated, so you do not need to repeat the code. Just use the handle (the address) of the destination axes as a parameter.

因此按钮1的代码

%// get the folder location and list of files
d= uigetdir(pwd, 'Select a folder');
fileList = dir(fullfile(d, '*.txt'));

hfig = ancestor(gcbo,'figure') ;        %// find the handle of the current figure
hax = findobj( hfig , 'type', 'axes') ; %// find the handle of the axes inside the main figure

setappdata( hfig , 'fileList' , fileList ) ; %// save the file list in the figure application data

plot_files( fileList , hax ) %// call the ploting function, specifying the CURRENT axes as destination

按钮2的代码

hfig = ancestor(gcbo,'figure') ;                %// find the handle of the current figure
fileList = getappdata( hfig , 'fileList' ) ;    %// retrieve the file list in the figure application data
hfig2 = figure ;                                %// create a new figure
hax2 = axes('Parent',hfig2) ;                   %// create an axes in the new figure
plot_files( fileList , hax2 ) %// call the ploting function, specifying the NEW axes as destination*

.m文件末尾的某处,放置绘图功能:

And somewhere at the end of your .m file, place the plotting function:

%% // Code for function to place somewhere at the bottom of your .m file
function plot_files( fileList , selectedAxe )
    len = length(files);
    linecolors = jet(len);

    for i = 1:len
        a = files(i).name;
        filename{i} = a;
        path = [d,'\',a];
        data = dlmread(path);
        plot(data(:,1), data(:,2),'color',linecolors(i,:),'linewidth',2,'Parent',selectedAxe );
        hold on;
    end

    hold off;
    xlabel('Intensity','fontweight','bold');
    ylabel('Wave Number','fontweight','bold');
    title('Spectra Plot','fontweight','bold','fontsize',14);
    legend(filename,'Interpreter','none', 'location', 'southoutside');
end

注意:根据代码中其他函数的定义方式,您可能必须删除绘图函数的最后一个end. (如果所有功能都以end结尾,请保留此功能,否则将其删除).

note: depending how your other functions are defined in the code you may have to remove the last end of the plotting function. (if all your functions finish with end, then keep this one, otherwise just remove it).

这篇关于(MATLAB)是否可以使用copyfile将从GUI中的轴导入的数据重新绘制到新图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆