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
Related
I used uigetfile to select multiple images. When I select the images and push the open button or press the enter key everything is okay. But when I instead select the images and double click on the selected images, I get this error:
Cell contents reference from a non-cell array object.
Error in Picketfence>insertpb_Callback (line 141)
file = fullfile(PathName,FileNames{i});
Here's my code:
c={'*.*', 'All Files(*.*)';'*.jpeg','figure (*.jpg)';'*.tif',...
'figure (*.tif)'};
[FileNames,PathName] = uigetfile(c, 'Select Images','MultiSelect','on');
if char(FileNames)
nfiles = length(FileNames);
handles.profile = zeros(1024,1024);
for i = 1:nfiles
file = fullfile(PathName,FileNames{i});
handles.profile = handles.profile+im2double(imread(file));
end
end
Why am I getting this error and how can I fix it?
The problem is that you can't select multiple files by double-click. When you select your files, then double-click on one of them, what's actually happening is that you are selecting only the one you double-clicked on. In other words, the first click of the double-click selects just that one, clearing the others.
When the GUI closes and returns to your code, you only have a single file selected, so FileNames isn't a cell array, just a character string. This is why the cell content indexing with {} fails.
A few more points about your code...
Your conditional check if char(FileNames) is wrong. The char function doesn't return logical (i.e. boolean) values. It converts things to character arrays. As per the documentation for uigetfile, the outputs will be zero when the selection is cancelled or the GUI closed, so the appropriate check in your case would be:
if ~isequal(FileNames, 0)
% Do your processing here...
else
% Nothing was selected; take some other action
end
You may want to account for the possibility of only 1 file being selected, thus having FileNames be a character array. The simplest way to do this is to first check if FileNames is a character array using ischar, and if so encapsulate it in a 1 element cell array (since your code expects cell arrays):
if ischar(FileNames)
FileNames = {FileNames};
end
Then you can do all your processing as you have it written above.
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.
I have a .m file written with definition of input & output variables along with calling of other function files which calculate the numeric output from provided numeric input.
I want to build a GUI in MATLAB for the same.
What I require is coding information for
1)Retrieving numeric data from 'edit text' component & pass this data assigned as input data
2)Set an action by clicking push button to run the program, calculate output from input & display the numeric values as output.
As far as I understand your question, it's pretty easy. I hope this will be helpful.
1. open guide by typing guide in command window.
2. click blank gui
3. guide window will open
4. click and drag a push button and an edit text.
5. click the editor
6. save your file
7. go to the follwing function
function pushbutton_Callback(hObject, eventdata, handles)
and write down this code below.
str = inputdlg('Enter numbers (seperated by commas)');
num = str2num(str{1});
a=num(:,1);
b=num(:,2);
ans=a+b; //or whatever you want to do!
caption = sprintf('your answer is %.2f',ans)
set(handles.edit,'string',caption)
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}]);
MATLAB has several selection-sensitive capabilities. For example, if you select some text and press F9, it evaluates your selection. (Unless you've remapped your keyboard settings.)
I'd like to be able to replicate this functionality with for a shortcut. So, for example, I want to click a shortcut that displays the current selection. My shortcut callback would be disp(GetSelection()).
But what goes into GetSelection?
Thanks to #Yair Altman's undocumented Matlab, I was able to figure out the java commands to make this work.
Put this in a shortcut (or a function that is called by the shortcut):
%# find the text area in the command window
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
try
cmdWin = jDesktop.getClient('Command Window');
jTextArea = cmdWin.getComponent(0).getViewport.getComponent(0);
catch
commandwindow;
jTextArea = jDesktop.getMainFrame.getFocusOwner;
end
%# read the current selection
jTxt = jTextArea.getSelectedText;
%# turn into Matlab text
currentSelection = jTxt.toCharArray'; %'
%# display
disp(currentSelection)
I don't believe there is any way to control or read the selection from the Matlab text editor, there is no mention of such an API on the Mathworks website (at least from a quick search on Google). If you want this functionality to enable more advanced text editing, then you might want to consider setting the .m file editor to an external editor (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/brxijcd.html). It may be possible to read the selection from a UIcontrol in a custom GUI, but I don't think this is what you want.
In case you want to use something like this but with text highlighted in the editor rather than in the command window.
I use the following code in order to be able to quickly check the nnz() of a variable, although you can change the code in the nested try-catch to whatever you need.
Lastly, I created a shortcut with this code in the top right of Matlab, which I access quickly by pressing Alt-1.
try
activeEditor = matlab.desktop.editor.getActive;
currentSelection = activeEditor.SelectedText;
try
eval(sprintf('val = nnz(%s);',currentSelection))
disp(sprintf('>> nnz(%s) = %s',currentSelection,num2str(val)))
catch ex
disp(ex.message)
end
catch ex
disp(ex.message)
end