问题
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.
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');
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');
Can anybody help?
Thanks
Vera
回答1:
Edit: 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.
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.
initial answer:
You can save parameters or variables in the figure application data, using the setappdata and getappdata commands.
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).
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.
So Code for button 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
Code for button 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*
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
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).
回答2:
You can use savefig command in Matlab to save the figure and the pressing of the button simply loading that figure. Write
savefig('figure1.fig')
at the end of the first bit of code. And just
openfig('figure1.fig','new')
in the second bit of code.
来源:https://stackoverflow.com/questions/28704239/matlab-can-i-re-plot-imported-data-from-axes-in-gui-to-new-figure-using-copyfi