MATLAB Answers

what command or box do i use to get a dynamic input box on app design GUI

1 view (last 30 days)
I am trying to find a command or input box where I can make dynamic inputs to plort on the graph. I want it to take in the value of the no of sensors in rthe GUI and the then open an input box asking for the coorinates of each sensor
for example :-
No of sensors= 3
an input box asking for 3 sets of X and Y coordinates to plot on the graph
No of sensors= 4
an input box asking for 4 sets of X and Y coordinates to plot on the graph
  1 Comment
Jeet Shetty
Jeet Shetty on 20 Jul 2021
I have figured out how to edit the uitable but i am trying to figure out how can i save the input vales, i added another add button to save the program but it is not working out and how can i plot the x and y values from the table using a scatter graph ?
properties (Access = private)
sensor_table = gobjects(1,1); %initialize as graphics object
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: RunButton
function RunButtonPushed(app, event)
GridWidth = app.GridWidthEditField.Value;
GridHeight = app.GridHeightEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value;
Ycoordinate = app.YcoordinateEditField.Value;
Lengthofcell = app.LengthofcellEditField.Value;
Gridx = Xcoordinate : Lengthofcell : GridWidth+Xcoordinate;
Gridy = Ycoordinate : Lengthofcell : GridHeight+Ycoordinate;
if Gridx(end) >= platewidth
error('Grid x (%d) + grid width (%d) >= plate width (%d)', Xcoordinate, GridWidth, platewidth);
if Gridy(end) >= plateheight
error('Grid y (%d) + grid height (%d) >= plate height (%d)', Ycoordinate, GridHeight, plateheight);
Mx = length(Gridx);
My = length(Gridy);
[x, y] = meshgrid(Gridx, Gridy);
%create the list of text
labels = string(1:Mx*My).';
%insert the labels
hold(app.UIAxes, 'on')
scatter(app.UIAxes, x, y, "O", "MarkerFaceColor", 'r')
text(app.UIAxes, x(:), y(:), labels, 'HorizontalAlignment', 'left', 'verticalalignment', 'bottom')
%calculte the grid lines
grid1x = [Gridx;Gridx];
grid1y = [Gridy(1); Gridy(end)];
grid2x = [Gridx(1); Gridx(end)];
grid2y = [Gridy; Gridy].';
plot(app.UIAxes, grid1x, grid1y, "Color", 'b');
plot(app.UIAxes, grid2x, grid2y, "Color", 'b');
rectangle(app.UIAxes, 'Position', [0 0 platewidth plateheight],'LineWidth', 5);
box(app.UIAxes, 'on')
grid(app.UIAxes, 'on')
xlim(app.UIAxes,[0 platewidth])
ylim(app.UIAxes,[0 plateheight])
hold(app.UIAxes, 'off')
N = app.NoofSensorsEditField.Value;
sensor_number = (1:N).';
X_coordinate = zeros(N,1);
Y_coordinate = zeros(N,1);
T = table(sensor_number, X_coordinate, Y_coordinate);
app.sensor_table = uitable(app.UIFigure, 'Data', T);
app.sensor_table.ColumnEditable = true;
% Button pushed function: AddButton
function AddButtonPushed(app, event)
app.sensor_values=uitable(app.UIFigure, 'Data', T);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Jul 2021
You are using App Designer, so you are probably using uifigure() as your base.
So you can have your code call uieditfield() to dynamically place an edit box onto the container.
You can configure its ValueChangedFcn to invoke a callback when the user enters data into it. Or not, if you have some other way for the user to invoke action (which is probably the case since you want the user to enter in all the values before doing anything.)
If you do configure a callback, you would provide the handle to a function that you had already written. You would not try to write callback code dynamically! Instead, have all of the fields use the same callback code, with the callback distinguishing between the fields by their Tag or by some UserData you attached to the field.
If you do not have a maximum number of sensors that the user can enter, then you need to design in some kind of scroll box for the inputs. But if you are headed towards that, then uitable() is often an easier approach.
If you have a small fixed maximum number of sensors, then often the easiest approach is to design all of them with positions and callbacks and whatever ahead of time, but set them all to be invisible at the start, and then turn on only as many as need. This does not involve creating any objects dynamically, just making existing objects visible or invisible.

Sign in to comment.

More Answers (1)

Jai Khurana
Jai Khurana on 2 Jul 2021
Jeet Shetty
Jeet Shetty on 5 Jul 2021
Yeah Uitable isn't the thing we're looking for That code didn't really output much after I rechecked errors But I researched on uitable that isn't what i need inputdlg is something that i was trying But I don't know what's the next step

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!