Handling carriage return/line feed in MATLAB GUI user controls - matlab

I have a MATLAB program I am developing in order to do some image processing stuff and need to use a user control into a MATLAB GUI user interface I created ad-hoc.
This user control is a List Box and there I would like to insert some text. Well the problem is not that I cannot put text there, I can do that using this call:
set(handles.mylistbox, 'String', 'MyStringToPrint');
Well the problem is that this call does not let me insert many lines in my list box but just overwrite the previous one.
I wish to find a way to let my code insert the new text in a new line. This should not be so difficult to do and it is also a simple pattern:
texttoprint = 'My text to add'
oldtext = get(handles.MyListBox, 'String') %Holding the previous text here
set(handles.MyListBox, 'String', [oldtext '\n' texttoprint]) %Setting (no line feed printed)
set(handles.MyListBox, 'String', [oldtext char(10) texttoprint]) %Setting (this fails too)
Well it is ok and it does not raise any error BUT, \n DOES NOT WORK.
I do not have any new line... BUT NEED TO!!!!
How should I solve this?
The problem is that I need to print text in this user control, not on the MATLAB commandline (that is very simple just by doing sprintf()).
What to do? Thank you

For a listbox, set the string property to a cell
set(myListboxHandle,'String',{'myFirstLine';'mySecondLine'})
If you want to add another line, call
contents = get(myListboxHandle,'String');
set(myListboxHandle,[contents;{'another line'}])
For multiline text in GUIs otherwise, use char(10) instead of \n, i.e.
set(someUiControlHandle,'String',sprintf('my first line%smy second line',char(10)))

When working with list boxes it's usually easier to deal with the options as a cell array of strings. So, you would initialize your list box as follows:
set(handles.MyListBox,'String',{'Option 1'});
And then you can add options to your list box like so:
newOption = 'Option 2';
oldOptions = get(handles.MyListBox,'String');
set(handles.MyListBox,'String',[oldOptions; {newOption}]);

Related

Matlab imline and text

Thank you for your answer. I should have been more clear. I need a text object for every line. Here's how far I got:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy = imline;
addNewPositionCallback(xy,#(pos) disp_slope(pos));
end
function disp_slope(pos)
delete(findobj(gca,'Type','text'));
text((pos(1)+pos(2))/2,(pos(3)+pos(4))/2,['\DeltaY/\DeltaX = ',num2str((pos(4)-pos(3))/(pos(2)-pos(1))),...
' [\DeltaX = ',num2str(pos(2)-pos(1)),', \DeltaY = ',num2str((pos(4)-pos(3))),']']);
So, each toggle on the toggle button in the figure will throw in a draggable/resizable line and as I move the line the slope shows and updates, which looks pretty close to what I want. There are, however, two issues:
It deletes all other text boxes except for the very last (current)
text box of the line I'm moving around. I want to keep the last
value remained and shown for all existing lines.
If I delete a line by right-clicking on the line and choose
"delete", it deletes the line, but not the text box and rightfully
so. Now I have a text box that shows the slope of the line that
exists no longer in the figure. I want the text box to disappear
along with the line.
I'm having these problems and really not going anywhere because imline object behaves so much different than other typical objects and also the concept of the addNewPositionCallback is quite convoluted.
Please, somebody enlighten me on this.
Many thanks in advance,
Eric
Previous message:
I create a figure with toggle button on its toolbar by uitoggletool. The callback function for it is shown below:
function calc_slope(handle,event)
on = get(handle,'State');
if strcmpi(on,'on') || strcmpi(on,'off'),
xy=imline;
addNewPositionCallback(xy,#(xy)...
title(['\DeltaY/\DeltaX = ',num2str((xy(4)-xy(3))/(xy(2)-xy(1))),...
'[\DeltaX = ',num2str(xy(2)-xy(1)),...
',\DeltaY = ',num2str((xy(4)-xy(3))),']']));
end
As you can see, the example outputs the position data in the title of a figure using the 'title' command.
Is there a way to output this in a text box using the 'text' command?
I want to display the slope next to the line drawn.
Also, it will be great if the text box also gets deleted together with the associated line.
Please, help.
Thank you,
Eric

Error using whitespace in MATLAB GUI label

I am writing a gui now which contains a popupmenu. This pop up menu should show different names according to cell array called titles and looks like this:
handles.titles={'time','velocity','angular velocity'};
Next, when i click on the popupmenu, i want it to plot the column which connected to this title.
so if i clicked on the 2nd popupmenu option i would like to get graph of velocity vs time.
handles.Parameter_Menu=hObject;
axes(handles.low_axis);
guidata(hObject,handles)
x_axis=xlim([handles.top_axis]);
set(hObject,'string',handles.titles(1:size(handles.matrix.Data,2)));
channel = get(hObject,'Value');
title_channel=handles.titles{channel}
plot(handles.(handles.titles{1}),handles.(handles.titles{channel}));
text=['graph of ' handles.titles2{channel} ' vs ' handles.titles2{1}];
title(text,'fontsize',12)
set(gca,'fontsize',10)
grid on
The problem occurs when i try to use title{3} because it has a space between the words.
Of course i could write it like this angular_velocity but then when i use it in title of the graph i recieve the letter "v" small because of the _ before it.
Is there any option to make it work with space or option to use it with _ but to avoid its effect in the title?
To make the title in plot displays exactly _, you can insert \ before _ (similar to latex):
handles.titles={'time','velocity','angular\_velocity'};
UPDATE:
since you are not only using the titles{i} for plot, but also use it as a struct's fieldname in other commands, then there is no way to have space, because a struct's fieldname must satisfy some conditions: Valid field names begin with a letter, and can contain letters, digits, and underscores.
So, you must use titles{3} = 'angular_velocity' to make other operations work correctly, and set Interpreter property of the title to be none to make the plot's title display as typed (default is TeX Markup, that's why i used \_ for _ above):
title(handle_of_something, titles{3}, 'Interpreter','none')

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.

result of a callback function in matlab gui

I have created a GUI in matlab with few buttons. Each button when clicked performs a certain function. Now I want to display the calculations being performed in the function to be displayed in a static textbox in the GUI. Is that possible? I am able to display it in the command window by removing the semicolon (;) at the end of the statement but I want it to be displayed in the GUI like a log.
Now when I click the "match" button the following function is called and I want to display whether it matches or not in the GUI in a textbox. Is that possible?
function matchin
[image1, pathname]= uigetfile('*.bmp','Open An image');
Directory = fullfile ('F:','matlab','bin');
D = dir(fullfile(Directory,'*.bmp'));
%imcell = {D.name}';
for i = 1:numel(D)
if strcmp(image1,D(i).name)
disp('matched');
else
disp('not matched');
end
end;
I replaced the code with the one specified in the answer. Without using the text box the output in the matlab command window when I select the second file is
not matched
matched
not matched
not matched
not matched
But if I use a static textbox only the last line is being displayed. How can I display all the 5 lines totally?
Yes, you can do that.
A good practice would be to save a structure with all the handles of your GUI elements by using the function guidata. For more information on this see this link.
Then in your callback you can retrieve this structure, for instance by:
handles = guidata(gcbo);
Then you can set the value of the textbox you want by replacing
if strcmp(image1,D(i).name)
disp('matched');
else
disp('not matched');
end
with:
if strcmp(image1,D(i).name)
set(handles.handle_of_textbox,'String','matched');
else
set(handles.handle_of_textbox,'String','not matched');
end

Matlab text object carriage return issue + text outside graph

I'm trying to show some text on top of a plot. Using
text(13,15200,('~ 12h'),'HorizontalAlignment','right','VerticalAlignment','middle','FontSize',10,'FontAngle','italic');
I can create the text box how I want it. But a thing I didn't manage to do is to create a carriage return within that text object. Already tried to add '\r' or '\n' but the only thing I get is the display of '\r' or '\n' with out breaking the line. Any ideas?
And i now that text object are used to put text WITHIN axes. But I am still trying to find a way to put some text outside the grap. Searching stackoverflow I found this Post about using UICONTROL. It works great but when I export to EPS the text seems to be in pixel format and NOT vector - so it looks really bad. Is there another way besides using uicontrol??
Thanks a lot for help!
In text that is not interpreted, you can specify line breaks using the carriage return character, which has the ascii code 10.
text(x,y,sprintf('break after this%snew line here',char(10)));
To allow text outside axes, you need to turn off clipping for the text object
text(x,y,.....,'clipping','off')