pressing key within mfile function
1 view (last 30 days)
Show older comments
Hi guys,I'm trying to change flag value by mouse click or pressing key during the running of my function mfile.
I could do it in a simple mfile. But when I put these command within function, pressing key did not work in order to change the flag (0 to 1 or 1 to 0). Could you please help me?
I tried different methods like below:
Method 1.
set(gcf,'keypress','k=get(gcf,''currentchar'');');
Method 2.
uicontrol('String','start','Position', [400 20 120 20],'CallBack','sflag=1;');
Both of them work in simple mfile. But within function m. file it doesn't work.
0 Comments
Answers (1)
Nick Counts
on 5 Nov 2016
Afsoon,
Without your function's code, it's a bit of a guess, but I think your issue probably has to do with variable workspaces (other languages call this the variable's 'scope')
Basically, when you are in a script, any variable you create is in the 'base' workspace and is available at the command window and to any other script file.
You can think of script files as just executing each line of code sequentially in the Command Window.
Functions have their own variable workspace (scope). Code executing inside a function can not see variables in the base workspace, or in any other function's workspace.
There are some exceptions to this, and more details. To understand variable scoping/workspaces, I recommend the following:
Those will help you understand what variables are available in your functions. You will have to pass your variable between functions as an argument, or store it in something like appdata (see setappdata and getappdata).
If you are building a GUI using guide, this is what the handles structure is. You can add a field to handles, update the structure ( guidata(hObject, handles); ), and then access it from any callback that has been passed handles as an argument.
Hopefully that wasn't too confusing and gets you pointed in the right direction.
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!