How to create a GUI in matlab where the user can select a single testcase he wants to run?

Hello,
I'm trying to create a GUI for Model In loop Regression testing using the Matlab script. Here I have 100 testcases,
I want to select single testcase out of 100 and that selected testcase should run. I want the format to look like below
  1. Testcase 1
(i)TestCase1a
(ii)TestCase2a
2. TestCase 2
(i)TestCase1b
(ii)TestCse2b
etc
Currently, I'm using prompt command to enter the Testcase number I want to run. But I want to improvise this in GUI format which has all the testcases for the user selection.
Any inputs greatly appreciated!
Thank you!

4 Comments

Hello,
Thank you for the suggestion.
I tried to use the uitree in app designer.
Currently, I have only taken 4 testcases to start with. Below are the parent nodes(no child nodes yet)
  1. Sensor1 Fault
  2. Sensor 2 Fault
  3. Sensor3 Fault
  4. Sensor 4 Fault
So, I have created 4 nodes for the above. I have testcase number for the above fault cases already which will be executed by the variable "TC_RunNum". But How to assign the TC_RunNum in the app script for it to run?
I have added the call back function(Selection changed Function). But not sure what will be the format to assign the testcase number(TC_RunNum) for each in the call back function. For example: Sensor 1 Fault - TC_RunNum is 10, sensor 2 fault is TC_RunNum is 11 etc.
Below is the callback funtion it generated when asked for one:
% Selection changed function: Tree
function TreeSelectionChanged(app, event)
selectedNodes = app.Tree.SelectedNodes;
end
end
You're welcome!
Here is a code structure you can use (you should take care that only one node is selected at a time, too):
function TreeSelectionChanged(app, event)
switch app.Tree.SelectedNodes
case app.Sensor1FaultNode % <- the name of Sensor1 Fault Node in your app
TC_RunNum = 10;
case app.Sensor2FaultNode % <- the name of Sensor2 Fault Node in your app
TC_RunNum = 11;
% etc.
end
% then run your script here
end
I have added the switch cases in the mlapp file
I tried to call this mlapp file from .m script by running
run("app1.mlapp");
Looks like it is not running that command and going through the other lines of the .m script. Below is the .m script(just initial couple of lines)
clc;
run("app1.mlapp");
TC_size = size(TC_data);
TC_row = TC_size(1);
TC_len = length(TC_data(:,TC_RunNum).Variables);
After running the above lines, it throughs the below error:
Unrecognized function or variable 'TC_RunNum'.
Error in RunSelectTC (line 40)
TC_len = length(TC_data(:,TC_RunNum).Variables);
I have mentioned the TC_RunNum in the .mlapp file like below
methods (Access = private)
% Selection changed function: Tree
function TreeSelectionChanged(app, event)
switch app.Tree.SelectedNodes
case app.FNRFltNode
TC_RunNum = 25;
case app.StrFltNode %
TC_RunNum = 28;
case app.PrkbrkswfltNode
TC_RunNum = 40;
case app.PBSolfltNode
TC_RunNum = 41;
end
Quesions:
  1. How to call the mlapp file in the .m script? so that user can select the testcase.
  2. The mlapp file which I'm creating is only for the testcase selection. Once the user selects the testcase, it should come back to the .m script. Should I add any logic over here in the mlapp script to go back to .m script? Or something to do add in the m script?
Thank you!!

Sign in to comment.

 Accepted Answer

I misunderstood the situation before; I thought you were running a script from an app, not the other way around.
If all you need is a single selection to set a variable in your script, you can run a simple GUI (not an app) from your script. Something like this would work. Here I'm using a popupmenu, but you could use a uitree.
function out = get_case_number()
f = figure( ...
'NumberTitle','off', ...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'Name','Select Case', ...
'Menubar','none', ...
'Toolbar','none', ...
'Units','pixels', ...
'Position',[300 400 200 140]);
p = uicontrol(f, ...
'Style','popupmenu', ...
'String',{'FNRFlt';'StrFlt';'Prkbrkswflt';'PBSolflt'}, ... % specify the case names
'UserData',[25;28;40;41], ... % and corresponding TC_RunNum value
'Value',1, ...
'Units','pixels', ...
'Position',[20 100 80 22]);
b = uicontrol(f, ...
'Style','pushbutton', ...
'String','Select', ...
'Units','pixels', ...
'Position',[30 20 65 22], ...
'Callback',@cb_button);
out = [];
uiwait(f);
function cb_button(~,~)
UD = get(p,'UserData');
out = UD(get(p,'Value'));
delete(f);
end
end
Then in your script, at the point where the user should make a selection, just say:
TC_RunNum = get_case_number();
and continue with the rest of the script, e.g.:
TC_size = size(TC_data);
TC_row = TC_size(1);
TC_RunNum = get_case_number();
TC_len = length(TC_data(:,TC_RunNum).Variables);

12 Comments

You're welcome! Any questions, please let me know.
Hello,
I tried to add listbox instead of popup menu. But how to add the dropdown within the same uicontrol? Like for some cases I need to have dropdown and for some cases no need.
I have added the uidropdown below:
function out = get_case_number()
f = figure( ...
'NumberTitle','off',...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'Name','Select TestCase', ...
'Menubar','none', ...
'Toolbar','none', ...
'Units','pixels', ...
'Position',[300 200 200 140]);
p = uicontrol(f, ...
'Style','listbox', ...
'String',{'sensr1flt';'sensr2flt';'sensr3flt';'sensr4flt'}, ... % specify the case names
'UserData',[25;28;40;41], ... % and corresponding TC_RunNum value
'Value',1, ...
'Units','pixels', ...
'Position',[20 100 80 60]);
a = uidropdown(f,"Items",["sensr1flt","sensr2flt","sensr3flt","sensr4flt"], ...
'UserData',[21;22;23;24], ...
'Position',[20 100 80 60]);
It is giving the below error:
Error using uidropdown (line 41)
Functionality not supported with figures created with the figure function.
Error in get_case_number (line 22)
a = uidropdown(f,"Items",["sensr1flt","sensr2flt","sensr3flt","sensr4flt"], ...
It is not allowing me to use the same figure. Looks like I have to use dropdown within the same uicontrol ,How to use the dropdown within the same uicontrol?
Thank you
uidropdowns are not supported in figures, only in uifigures.
In a figure (as opposed to a uifigure), use a 'popupmenu'-style uicontrol instead of a uidropdown (note the different property names, e.g., String instead of Items):
a = uicontrol(f, ...
'Style','popupmenu', ...
'String',["sensr1flt","sensr2flt","sensr3flt","sensr4flt"], ...
'UserData',[21;22;23;24], ...
'Position',[20 100 80 22]);
Or you can switch to using a uifigure in which case you can use a uidropdown or a uicontrol:
f = uifigure( ...
'NumberTitle','off',...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'Name','Select TestCase', ...
'Menubar','none', ...
'Toolbar','none', ...
'Units','pixels', ...
'Position',[300 200 200 140]);
a = uidropdown(f, ...
"Items",["sensr1flt","sensr2flt","sensr3flt","sensr4flt"], ...
'UserData',[21;22;23;24], ...
'Position',[20 100 80 22]);
UI components for uifigures:
UIControls for figures or uifigures:
Thank you!
I have another question. How to reduce the distance between the top edge of the Listbox and the inner top edge of the figure?
Can I insert an image inside the figure (which is outside of the listbox)? because as we have the uicontrol in this figure. Image insertion is supported?
To reduce the space, adjust the Position(s). Position is [x y width height].
To insert an image, it's probably best to switch to uifigure and add a uiimage component.
Yea, I tried to adjust the position of
f = figure( ...
'NumberTitle','off',...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'Name','Select TestCase', ...
'Menubar','none', ...
'Toolbar','none', ...
'Units','pixels', ...
'Position',[300 100 150 500]);
But the above position is adjusting the figure height. But not the distance between figure and listbox
Used uifigure and uilistbox. That solved my issue. Thanks!
You're welcome!
In case the spacing is still an issue, adjust the Position of the listbox instead of the figure. In particular, to decrease the space above the listbox, increase its y-component, i.e., Position(2).
Hello,
I have attached the .m file.
So, after I changed figure to uifigure, uicontrol to uilistbox, for button selection changed that to uibutton.
I commented out the uiwait(f) as the script is stopping at uiwait after the above changes.
My main script(where I called the .m file) is throwing an error :
Error using RunSelectTC (line 160)
Invalid StopTime specified in the Configuration Parameters dialog for block diagram 'Regression'
In Configuration parameters dialog, the solver is set to Fixed step.
The main script is working fine, if I revert back the changes that is uifigure to figure, uilistbox to uicontrol.
The advantage of using the uifigure,uilistbox. The GUI looks neat
See the attached modifed m-file.
You need the uiwait in order to be able to return the selected value. But also the button needs a callback, which was missing for uibutton. I put that in place.
Also, Value means different things for a uicontrol button vs a uibutton (as described in the links I posted earlier), so I fixed the button callback to work for a uibutton.
I also repositioned the objects to have a reasonable initial arrangement.
Thank you so much!!
idx = find(strcmp(get(p,'Items'),get(p,'Value')),1);
Sorry, I did not understand 'Value' in the get command

Sign in to comment.

More Answers (0)

Categories

Find more on Update figure-Based Apps in Help Center and File Exchange

Asked:

on 15 Mar 2024

Commented:

on 21 Mar 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!