Save figure userdata to a workspace variable

조회 수: 18 (최근 30일)
Johannes Hougaard
Johannes Hougaard 2020년 4월 1일
댓글: Johannes Hougaard 2020년 4월 2일
I have assigned some the data and metadata (and some additional stuff) as 'Userdata' to a figure in order to assure that the graphic description of my data matches the actual data behind the graphics.
When I open the figure the original data are easily retrievable by:
graphdata = get(fig_handle,'UserData');
But, what I wish to do is to create a button in my figure which allows me to 'export' the userdata to workspace. Is that possible by creating/assigning a ButtonDownFcn to a pushbutton uicontrol? Preferably with a dialogue for naming the variable.
If possible I'd prefer assigning something generic to the ButtonDownFcn to assure that it works for other users even without access to my GIT or my local/personal functions.
fig_handle = figure;
myData.xData = rand(10,12000);
myData.yData = randn(10,1);
myData.sampleNames = {'Sample01','Sample02','Sample03','Sample04','Sample05',...
'Sample06','Sample07','Sample08','Sample09','Sample10'};
plot(myData.xData')
legend(myData.sampleNames);
set(fig_handle,'UserData',myData)
btn = uicontrol('Parent',gcf,'Style','pushbutton','string','Export userdata');
set(btn,'ButtonDownFcn',@somethingsomething)
savefig(fig_handle,'figure_with_myData.fig')
...i.e. what to do with the @somethingsomething in order for the button to extract userdata to a user defined variable in the base workspace.

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 1일
Johannes - instead of using the ButtonDownFcn for the button, just use Callback. In this example, when the user presses the button, the callback fires and the userData is saved to a mat file (it can be adapted to save to the workspace if needed):
function FigureWithExportButton
fig_handle = figure;
myData.xData = rand(10,12000);
myData.yData = randn(10,1);
myData.sampleNames = {'Sample01','Sample02','Sample03','Sample04','Sample05',...
'Sample06','Sample07','Sample08','Sample09','Sample10'};
plot(myData.xData')
legend(myData.sampleNames);
set(fig_handle,'UserData',myData)
btn = uicontrol('Parent',gcf,'Style','pushbutton','string','Export userdata', 'Callback', @exportDataCallback);
function exportDataCallback(hObject, eventdata)
userData = get(hObject, 'UserData');
filename = [char(inputdlg('Please enter a file name:', 'Export UserData')) '.mat'];
save(filename, 'userData');
end
end
  댓글 수: 1
Johannes Hougaard
Johannes Hougaard 2020년 4월 2일
Thank you Geoff.
That was really helpful.
I ended up using a slightly moderated version in order to stay clear of a 'personal' function (the exportDataCallback) as I generated the figure in a script rather than a function.
But this:
set(btn,'Callback',@(x,y)cellfun(@(varname)assignin('base',varname,get(get(x,'Parent'),'UserData')),inputdlg('Enter Variable Name')));
did the trick for me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by