MATLAB Answers

How to use keypressfcn to allow the user only to copy from an edittext?

3 views (last 30 days)
theblueeyeswhitedragon on 15 Apr 2018
function [] = GUI_15()
S.fh = figure('units','pixels',...
'position',[300 300 400 120],...
S.ed = uicontrol('style','edit',...
'position',[30 70 340 30],...
'string','This text can be copied but not changed');
S.pb = uicontrol('style','push',...
'position',[30 30 340 30],...
'string','Print to screen');
set([S.ed,S.pb],{'callback'},{{@ed_call,S};{@pb_call,S}}) % Set callbacks.
set(S.ed,'keypressfcn',{@ed_kpfcn,S}) % set keypressfcn.
function [] = pb_call(varargin)
% callback for pushbutton
S = varargin{3}; % Get the structure.
disp(get(S.ed,'string')) % Print to the command line.
function [] = ed_call(varargin)
% Callback for edit
S = varargin{3}; % Get the structure.
set (S.ed,'string','This text can be copied but not changed');
function [] = ed_kpfcn(varargin)
% Keypressfcn for edit
[K,S] = varargin{[2 3]};
if isempty(K.Modifier)
set (S.ed,'string','This text can be copied but not changed');
elseif ~strcmp(K.Key,'c') && ~strcmp(K.Modifier{1},'control')
set (S.ed,'string','This text can be copied but not changed');
I have just started learning about GUI in MATLAB and I found this code in LinkExchange, which basically creates an edit-text and push-button and does not allow the user to make modifications to the edit-text but just copy it. The code makes sense till the last function "ed_kpfcn". I don't understand the use of varargin in this case. Also, why for seeing if the 'c' button is pressed we refer to the Key field and for 'control' to the Modifier field?
Also, if you know any other resources for getting started with GUIs in MATLAB, besides the ones provided by Mathworks I would appreciate if you shared them here. Thank you.


Sign in to comment.

Answers (0)


Community Treasure Hunt

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

Start Hunting!