Dynamically updating text in Matlab GUI - matlab

I think it should be pretty simple what I want to do, basically I have one edit box that displays a value in percentages and another that I want to update to display raw values. I've tried using the following code under the edit1 (percent) callback:
currentKey = str2num(get(gcf,'CurrentKey'));
percent = str2num(get(handles.edit1,'String'));
if ~isnan(currentKey) && ~isnan(percent) && 0<=percent && percent<=100
set(handles.edit2,'String',num2str(2*percent))
end
But it will only update the second edit box if I first click outside of the first one. Anyone have an idea of what I should be doing?
Thanks!

I think this link should help you:
How can I make the text that I enter into an edit text box update dynamically?
Solution:
This enhancement has been incorporated in Release 2011a (R2011a). For previous product releases, read below for any possible workarounds:
This is expected behavior of the Edit Box UICONTROL in MATLAB.
You can try using the 'keypressfcn' to grab the keyboard input. The attached two files demonstrate the ability of real-time text update. As you enter text into the upper edit box, the text will be copied to the edit box beneath it as you enter.
Please download the following two files:
test_keypressfcn.m
test_keypressfcn.fig
Execute the program.
A GUI will appear. Enter text in the upper edit box displayed in the GUI.
Observe the text in the lower editbox is updated dynamically or in real-time as you enter test in the upper edit box.
Please note that this will work only for text that is continuously entered in the editbox. If you type in between words already entered in the editbox the gui will not perform as expected. You will need to implement logic similar to the one in this example to get the behavior that you desire.

Related

Visual Studio Code select same position above and below not the whole line before and after (see image)

I'm using visual studio code and run into a weird problem. I'm not sure how I got here - I could have accidently pressed a shortcut unknowingly.
I'm trying to select a phrase, link or anything that crosses multiple lines (whether the lines are true lines or due to word wrap). When I select multiple lines, it doesn't automatically select the text at the start and end between the two points. Rather, it just selects the length of text for that line and repeats it in the subsequent lines. See the image below to understand.
Image of issue
As you can see, I am trying to select the words from "the" to the end of "sub". Instead of selecting all the words between the two, it selects the text "the instru" and selects every line with the same amount of characters/length.
In order to show what I am expecting, I have pasted the text into Notepad and done the same thing.
What I am expecting
As you can see, all the words between "the" and "sub" are selected.
If anyone has any idea about how to fix this, I would be greatly appreciative.
Below is a copy of the text if the images don't display.
Follow the instructions below for a click guide to retire and/or add 'School'.
Best practice if there is a change in 'School' structure would be to 'retire' any existing school setup that is no longer required and add the new sub school information. The reason why we don't just edit existing school names (typically) is due to leaving historical data intact.
Try using ctrl+shift+P and typing "Toggle Column Selection Mode"

How do I access the highlighted text in a Matlab GUI edit box

I would like to have a text box and a button in my GUI. When the button is pressed, a history window will come up, and if the user selects a previous entry the text that they have highlighted in the edit box will be overwritten.
It should work like copy-pasting, whatever is selected in the history window should be pasted over what is selected, or the new text should be added wherever the cursor is.
Is there any way in Matlab to do this? Is it possible to access what is highlighted in an edit box?
With vanilla Matlab this isn't possible. It appear that Mathworks is in the process of expanding what they support with GUIs (survey 1, survey 2), but as of yet they don't allow this.
One possible workaround is using findjobj.m, by Yair Altman. He discusses edit boxes in this post
You can trace findjobj.m for your text box to find 1 or 2 lines of code that are needed so you don't have to carry around all 3,400 lines of it.
Then all you really need to do is get the selected indices and work from there.
javaHandle = findjobj(editBoxHandle);
startSelect = get(javaHandle,'SelectionStart');
endSelect = get(javaHandle,'SelectionEnd');
Once you have the indexes of what text is selected, it becomes almost trivial to replace that text with the new text.
text = editBoxHandle.String;
editBoxHandle.String = [text(1:startSelect) newText text(endSelect:end)];
One thing to note, when the user clicks the button the text box will lose focus, and it will no longer be clear what text is selected. You can remedy this by giving focus back to the text box, and re-selecting what was selected in the button's callback.
uicontrol(editBoxHandle); %Give focus to the edit box, selecting the entire text
javaHandle.select(startSelect,endSelect); %select/highlight the correct stuff
This will highlight the text that will be replaced with the users selection

Using matlab GUIDE to produce a modal dialog that records user inputs?

I am new to using the matlab GUIDE. I know there are some predefined dialog boxes such as inputdlg and msgbox and warndlg and so on and so forth that can easily be implemented into the command line without having to play around with too many things.
However, I was wondering whether it was possible to modify inputdlg in matlab guide? I am just trying to produce a simple dialog box that reads the user input and when the user clicks ok, it closes and records the inputs somewhere. Using inputdlg it is very easy to do so:
uiwait(msgbox(sprintf('Please enter your new references for each electrode.')));
prompt = {'Fp1','Fp1','F7','T3','T5'};
dlg_title = 'Input references';
num_lines = 1;
answer = inputdlg(prompt,dlg_title,num_lines );
The user enters a string for each option 'Fp1', 'F7' and so on and all these answers are recorded in "answer".
Now I have 2 problems:
I have 16 such inputs and if I put them all inside the same "prompt" then the dialog box runs off the screen - so I use prompt, prompt2 and prompt3 to split them up and record the answers. It works fine, but it would be better if I could arrange the input boxes side by side as you can edit/drag inside the matlab guide.
I want my dialog box to look as it does in this picture with the minus sign between the 2 cells where the user will enter something into both boxes. The first box in each line is actually equivalent to the prompt that I have specified above, but in this case the user will enter the string into the first cell rather than being prompted for it.
But I can't seem to figure out how to do this either using inputdlg itself and altering its properties or using guide to create a custom inputdlg.
Does anybody have any advice?
Update
I have added some lines of code in the correct places and I am able to now store the user input to a variable. However, I have around 32 edit boxes and my current method means I will have 32 different inputs... I do not want this, I want them all to be recorded inside the same array.
The code I added was in the edit box callback function:
input = get(hObject, 'String')
display(input)
assignin('base','input',input);
% Save the value
handles.trial = input
guidata(hObject,handles)
This is from editbox1 and I have tried to proceed as
input = get(hObject, 'String')
A(1,:) = input
assignin('base','A',A(1,:));
but in this case it returns A as a cell which has the value entered in the last edit box.
Can anyone help?
First you should make matlab wait for the user to respond. Locate the OpeningFcn of you GUI. The last line should look like that
% uiwait(handles.figure1);
You should uncommon the line. Next, you have to resume the UI when the user clicks OK. This is done by inserting the line
uiresume(handles.figure1);
in the OK button callback. When the users clicks OK, the OutputFcn will be called. There you can return the values you need via varargout cell array. Finally, in the last line of OutputFcn, you should close your GUI using
close(hObject)
All the names I used are the matlab's defaults. If you changed any of them, modify the code accordingly.

Matlab GUIDE Display Current Date in Edit Text box

I am making my very first GUI using GUIDE and I am running into a problem with one of my Edit Text boxes. The Edit Text box will require the user to enter a date using the mm/dd/yyyy format. I expect that the user will be entering the current date 99% of the time, so I would like to have the current date (in mm/dd/yyyy format) already entered in the text box for the user.
I was able to achieve something like this in Matlab's Command Window using
datestr(date, 'mm/dd/yyyy')
However, I am unsure where/how to implement this code into my GUI to get it to display the date. Any help would be greatly appreciated. Thanks!
If edit1 is the tag to the editbox, add this to OpeningFcn for the GUI -
set(handles.edit1,'String',cellstr(datestr(date, 'mm/dd/yyyy')));
Thus, today's date would show up in the editbox, once the GUI loads up.
If I understood your question right, you need to use the "String" property of your edit box with the output from datestr(date, 'mm/dd/yyyy').
For instance:
set(handles.EditBox,'String',datestr(date, 'mm/dd/yyyy'));
and that should do it. You can put this line in the Create Function of the edit box, so that when the user will open the GUI the text will already be there.
Hope that's what you meant!

How can i create a textarea that has read and write lines using gtk

Any one know how i can create a text area which displays lines of text which can not be edited but allows you to edit the bottom most line for text.
basically in a similar fashion to embedded consoles inside applications that allow you to run code direct on the application.
currently using a textview i can go and edit the code above and the output response are also editable.
It's possible using a GtkTextView, but not trivial. You have to create a tag that makes the text uneditable, and apply it from the beginning of the buffer to just before the end position. Then when you receive and process input, extend or re-apply the tag to cover that line of input and your program's response to it.
Here's an example of it being done:
creating the tag
applying the tag