Return value from 'get' into handles in GUI [R2017b, Windows7]
1 次查看(过去 30 天)
显示 更早的评论
Hello,
This question stems off of another question but I thought it would add more value to have it as a spate question.
I have a GUI produced in GUIDE. It has an axes object. On the axes are three line plots:
handles.P(1) = line(handles.Plot1, 'XData', chunktime', 'YData', normChunk1, 'Color', 'Blue', 'Tag', 'Ch1Line');
handles.P(2) = line(handles.Plot1, 'XData', chunktime', 'YData', normChunk2, 'Color', 'Green', 'Tag', 'Ch2Line');
handles.P(3) = line(handles.Plot1, 'XData', chunktime', 'YData', normChunk3, 'Color', 'Red', 'Tag', 'Ch3Line');
These plots are produced in a push button callback. When I then click on one of the lines I want to select the line and make the 'tag' property available in the handles structure so that I can test which line was clicked and then reverse the phase of the line. So I have a separate line of code by itself (not inside any other call back):
set(handles.P, 'ButtonDownFcn', {@LineSelected, handles.P}) % Use set command with pointer to 'LineSelected' function which has argument handles.P.
I then have the LineSelected function:
function LineSelected(hObject, eventdata, AllLines)
set(AllLines, 'LineWidth', 0.5);
set(hObject, 'LineWidth', 2.5);
handles.clickedLine = get(hObject, 'Tag');
guidata(hObject, handles);
When I run my application handles.clickedLine is not available. In debug mode I have also manually checked the handles structure and this does not exist. Any ideas how to get some property of the clicked line that I can then use to detect which line was clicked, preferably building on from my code I already have?
Kind regards, TJ
4 个评论
Guillaume
2018-1-9
编辑:Guillaume
2018-1-9
@Adam, no, AllLines should exist since the callback is defined as {@LineSelected, handles.P}. It will receive the value of handles.P.
handles is more of an issue. Indeed it should be retrieved prior to setting it. As it is either a structure with just one field is created or, in the unlikely event that LineSelected is a nested function, an outdated handle variable will be used.
Adam
2018-1-10
Ah yes, I never use the cell array form of defining a callback so I forgot how it works. When using function handles I explicitly include src and evt arguments when I need to add a further argument so I imagined handles.p to be the only argument passed down, which would then become hObject!
handles = guidata( hObject );
will retrieve the handles, in this case.
Matlab's way of happily just creating a new struct with one field (well, actually even just creating a new double variable too) rather than throwing an error has caught me out a few times when I have made a typo in a class object name too!
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!