title: Limiting string length in a Edit Box on Matlab user interfaces
body: <p>I inserted an Edit box in a Matlab user interface and I would like to limit the number of characters that an user can type. There is no obvious property on the Edit box (such as "max characters"). I tried to use the callback function, verifying if the current string size on the edit box was bigger than a limit I set and truncating the first characters, however the callback only acted when I clicked outside the edit box and then inside again.</p>
  
  <p>Do you have any idea of how to do this?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDITED</p>
  
  <p>As suggested by Amro, I tried placing a verification code inside the KeyPressFcn callback of the edit box. I typed the following code:</p>
  
  <pre><code>function prefix_edit_KeyPressFcn(hObject, eventdata, handles)
      text = get(hObject, 'String');
      if length(text) &gt; 15
          set(hObject, 'String', text(1:15));
      end
  </code></pre>
  
  <p>The problem is that the edit box string is only changed when I type something, press Enter and then try to type something again. It seems that the KeyPressFcn is only called after pressing Enter (as mentioned in the forum post that Amro suggested).</p>
  
  <p>The solution proposed in the forum seems too complicated, for such a simple task. Surely there has to be a more elegant way...</p>