I'm new to matlab and I was wondering if anyone could shed some light.
I am using Matlab R2017B and App Designer.
My question:
I have a panel in a GUI that is default set to invisible.
I also have a dropdown menu with a few values stored:
"Please Select"
"Panel 1"
"panel 2"
"panel 3"
etc...
when i select panel 1 from the dropdown I want that specific panel to then become visible. ie the callback:
function DropDownValueChanged(app, event)
app.Panel1.Visible = 'on';
this is all fine and works but I cant seem to get the next part right.
When i go back to the dropdown and select "panel 2" What i would like is for the program to then close the current panel and open the panel named "panel 2" by selecting that value from the dropdown.
Am I wrong in using visibility to define this?
How can i connect the dropdown values to corresponding panels in a more intuitive way?
I've been messing around with all sorts of tutorials but still cant get it to work
Thanks in advance
You need to read the value of the dropdown and use that to evaluate which panel should be displayed. Initially hide all the panels and then just make the panel of interest visible. My DropdownValueChanged function looks like this.
% Value changed function: DropDown
function DropDownValueChanged(app, event)
value = app.DropDown.Value;
% Hide all the panels
app.Panel1.Visible = 'off';
app.Panel2.Visible = 'off';
app.Panel3.Visible = 'off';
%If Panel 1 is selected, show panel 1
if strcmp(value,'Panel 1')
app.Panel1.Visible = 'on';
elseif strcmp(value,'Panel 2')
app.Panel2.Visible = 'on';
elseif strcmp(value,'Panel 3')
app.Panel3.Visible = 'on';
end
end
I also have the following in the startupFcn to hide all but the first panel.
% Code that executes after component creation
function startupFcn(app)
app.Panel1.Visible = 'on';
app.Panel2.Visible = 'off';
app.Panel3.Visible = 'off';
end
Related
In app designer, I have multiple drop-down menus that allow the user to select data values that are then plotted. The first time the 'plot' button is clicked, there are no issues with plotting only one selection from a menu, or plotting multiple data values from multiple drop-down menus. However, if the user changes a drop-down menu value, only one of the values is plotted unless the 'plot' button is clicked again.
I have tried multiple solutions, including using hold in various ways or by clearing the axis during the 'plot' button's initial run (e.g., by using cla) to essentially reset the 'plot' button function.
Sample code:
SelectedMetric = app.OsteoDropDown.Value;
[ColumnValue] = OsteometricFunc(SelectedMetric);
PopSelectionA = app.PopulationA.Value;
[RowValue] = PopulationAFunc(PopSelectionA);
x = 1:length(RowValue);
y = OsteoData(RowValue,ColumnValue);
%hold(app.UIAxes);
scatter(app.UIAxes,x,y,'g');
PopSelectionB = app.PopulationB.Value;
[RowValue] = PopulationAFunc(PopSelectionB);
x = 1:length(RowValue);
y = OsteoData(RowValue,ColumnValue);
hold(app.UIAxes);
scatter(app.UIAxes,x,y,'r');
First instance of clicking the 'Plot' button does everything correctly:
Second instance of clicking 'Plot' button, wherein only the drop-down for 'Population II' has been changed. The plot for 'Population I' is no longer visible, though it should be:
Clicking the 'plot' button again correctly plots all values, however:
How do I resolve this so that switching a single drop-down menu (or multiple drop-down menus, for that matter) automatically plot everything for all drop-down menu values?
I'm writing an application in MATLAB and want to update the look of it. is it possible to change the icons of the buttons in the toolbar in MATLAB code?
The code will be compiled and I am not using GUIDE; ideally there's a way to get the button handles and set each icon individually although I don't know how I would do this.
But with higher quality icons.
Yes, you can change the figure toolbar icons, or add your own.
I've detailed how to change the icon below, as well as other useful things to do with the toolbar whilst you're editing the properties anyway.
See the code comments for details.
Get the toolbar object
% Initialise some figure
fig = figure( 'Name', 'MyApp', 'NumberTitle', 'off' )
% Get the figure toolbar handle
tbar = findall( fig, 'tag', 'FigureToolBar' );
You can do findall(tbar) at this point to list the names of all the buttons
Hiding buttons
Let's say you want to hide the "new figure" button:
% Get the button from the tbar object
btn = findall( tbar, 'tag', 'Standard.NewFigure' )
% Set to not visible
set( btn, 'Visible', 'off' );
Changing callbacks
Let's say you want the print button to trigger a print-preview callback instead of printing directly (you could assign any custom callback function to any button):
% Get the button again
btn = findall( tbar, 'tag', 'Standard.PrintFigure' );
% Change the callback (and the tooltip to match)
set( btn, 'ClickedCallback', 'printpreview(gcbf)', ...
'TooltipString', 'Print preview' );
Changing the icon
At this point you can see all button attributes are editable, including the image as per your original question.
In particular, just change the CData property:
% Update the print button to have a print preview icon
% This should be a 16*16 RBG image matrix, use imread to get from file
img = imread( 'printpreview.bmp' )
% Assign CData property to button already acquired
set( btn, 'CData', img );
Output (I used a random print preview icon, seen 4th from the left):
Add new buttons
You can add new buttons by simply creating uipushtool objects (with CData property set for the icon image) with the tbar object as a parent.
Change the separators
The vertical grey separators can be added or removed (useful for creating your own button groups or when removing buttons). Simply set the 'Separator' property to 'off' or 'on', for a separator on the left side of a button.
For a compiled app, this may be against The MathWorks T&Cs, but this is the how to not the should you do this!
I made a GUI with two uipanels, each containing 4 image axes.
I plotted different figures on each panel and want to switch between panels by a push button.
For this I used the following:
Initially, I set uipanel2 to 'visible' 'on' and uipanel3 to 'visible' 'off';
Then when I push 'push button' it checks if uipanel is 'on' and turns on and off respectively.
% Code:
set(handles.uipanel2,'visible','on');
set(handles.uipanel3,'visible','off');
% When I push 'push button':
if strcmp(get(handles.uipanel2,'visible'),'on')
disp('panel-2 onn switching it off')
set(handles.uipanel2,'visible','off');
set(handles.uipanel3,'visible','on');
elseif strcmp(get(handles.uipanel3,'visible'),'on')
disp('panel-3 onn switching it off')
set(handles.uipanel3,'visible','off');
set(handles.uipanel2,'visible','on');
end
It doesn't work as expected I don't see panels switching.
For displaying images I used code like this:
% Panel-2
axes(handles.RCC);
imshow(img_RCC,lims);
axes(handles.LCC);
imshow(img_LCC,lims);
axes(handles.RML);
imshow(img_RML,lims);
axes(handles.LML);
imshow(img_LML,lims);
% Panel-3
axes(handles.RCC_Orig);
imshow(img_RCC,lims);
axes(handles.LCC_Orig);
imshow(img_LCC,lims);
axes(handles.RML_Orig);
imshow(img_RML,lims);
axes(handles.LML_Orig);
imshow(img_LML,lims);
Update: I can only see the GUI panel on top being 'not visible' and switching to 'visible'. The panel on bottom I think is still there but I don't know how to make it come on top
This is the bar graph that I want to display on the GUIDE GUI. I put this code into the OpeningFcn function of the GUIDE GUI, and what essentially happens is that the actual box section dedicated to the graph (its tagged "axes1") appears in the GUI window, but then another Figure window appears displaying the bar graph. How would I go about placing this bar graph into the GUIDE GUI in the space dedicated to the box axes1?
I do not need any push button trigger to display it. The graph should appear in its dedicated spot on the GUIDE GUI when the GUI window is open.
EDIT: This is the graph data I want to display. I was using the previous one as an example so I could learn from it. However, for some reason there is a problem of the below graph appearing twice in the window - it appears once, closes, and then appears again. How would I fix it so it only appears once? All of this is under OpeningFcn and I don't have additional code under CreateFcn.
dbedit = matfile('varDatabase.mat', 'Writable', true);
results_pData = dbedit.pData;
results_uData = dbedit.uData;
results_name = dbedit.name;
% Create data for each set of bars for data from each group
% i.e. [participant, population].
% Population is defined as the previous user data stored in its full in uData.
expSingle = [((results_pData(1,2)/7)*100), ((mean(results_uData(:,2))/7)*100)];
expConjugate = [((results_pData(1,3)/7)*100), ((mean(results_uData(:,3))/7)*100)];
ctlSingle = [((results_pData(1,4)/7)*100), ((mean(results_uData(:,4))/7)*100)];
ctlConjugate = [((results_pData(1,5)/7)*100), ((mean(results_uData(:,5))/7)*100)];
% Create a vertical bar chart using the bar function
bar(handles.axes1,1:2, [expSingle' expConjugate' ctlSingle' ctlConjugate'], 1)
% Set the axis limits
axis([0 2.8 0 100])
set(gca,'XTickLabel',{results_name,'Population'})
% Add title and axis labels
title('Proportion of Responses for Conjunctive vs. Single Choices')
xlabel('Entity')
ylabel('Proportion of Responses (%)')
% Add a legend
legend('Single Choice, Experimental', 'Conjugative Choice, Experimental',...
'Single Choice, Control', 'Conjugative Choice, Control')
Input would be greatly appreciated.
That's because of your call to figure right after the creation of your data, which indeed creates a new figure outside of your GUI.
To solve your problem remove that line, and consider adding the actual axes handles in the call to bar:
bar(handles.axes1,1:12, [measles' mumps' chickenPox'], 1)
Therefore your whole code could look like this:
%Create data for childhood disease cases
measles = [38556 24472 14556 18060 19549 8122 28541 7880 3283 4135 7953 1884];
mumps = [20178 23536 34561 37395 36072 32237 18597 9408 6005 6268 8963 13882];
chickenPox = [37140 32169 37533 39103 33244 23269 16737 5411 3435 6052 12825 23332];
%Create a vertical bar chart using the bar function
%// Remove the call to figure.
bar(handles.axes1,1:12, [measles' mumps' chickenPox'], 1)
% Set the axis limits
axis([0 13 0 40000])
set(gca, 'XTick', 1:12)
% Add title and axis labels
title('Childhood diseases by month')
xlabel('Month')
ylabel('Cases (in thousands)')
% Add a legend
legend('Measles', 'Mumps', 'Chicken pox')
That should work now
I want to have an "edit" box in a MATLAB GUI that says "TYPE SEARCH HERE".
When the user clicks inside the box I want the "TYPE SEARCH HERE" to disappear and give the user an empty edit box to start typing in...
Any ideas?
At least on my system, when I use the follow code to set up a user input box/window
prompt = 'Enter search terms:';
dlg_title = 'My input box';
num_lines = 1;
defAns = {'TYPE_SERACH_HERE'};
answer = inputdlg(prompt, dlg_title, num_lines, defAns);
the default text TYPE_SEARCH_HERE appears highlighted, so I can just start typing to replace it with what ever I want.
Edit Alternatively, if you have an existing uicontrol edit box you could do something like the following:
function hedit = drawbox()
hedit = uicontrol('Style', 'edit',...
'String', 'deafult',...
'Enable', 'inactive',...
'Callback', #print_string,...
'ButtonDownFcn', #clear);
end
function clear(hObj, event) %#ok<INUSD>
set(hObj, 'String', '', 'Enable', 'on');
uicontrol(hObj); % This activates the edit box and
% places the cursor in the box,
% ready for user input.
end
function print_string(hObj, event) %#ok<INUSD>
get(hObj, 'String')
end
Chris, you've got to click in the uicontrol border to make the ButtonDownFcn happen. It won't happen if you click inside the edit box
Okay, so I have a solution to the problem and it works flawlessly!!
However, I am quite upset because I have absolutely no idea WHY it works...
Create an edit text box in GUIDE and right click on it to open up property inspector.
add text "TYPE TEXT HERE" to "string" property
Find the property nammed "Enable" and switch it to "inactive"
Create a buttonDownFnc (can be done in property inspector also)
Use following code:
function myEditBoxTagGoesHere_ButtonDownFcn(hObject, eventdata, handles)
% Toggel the "Enable" state to ON
set(hObject, 'Enable', 'On');
% Create UI control
uicontrol(handles.myEditBoxTagGoesHere);
If someone could explain why uicontrol highlights the text upon left mouse click, that would be great!
Hooplator15, it works because edit texts are like push buttons when Enable is Off:
If Enable == 'on' (edit text Enable), the function _ButtonDownFcn executes on mouse press in 5 pixel border;
Otherwise, it executes on mouse press in 5 pixel border or over edit text, like a button.