Save figure userdata to a workspace variable
조회 수: 18 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!