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

35 views (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
sensor_values
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: RunButton
function RunButtonPushed(app, event)
platewidth=app.WidthEditField.Value;
plateheight=app.HeightEditField.Value;
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);
end
if Gridy(end) >= plateheight
error('Grid y (%d) + grid height (%d) >= plate height (%d)', Ycoordinate, GridHeight, plateheight);
end
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])
xticks(0:Xcoordinate:platewidth)
yticks(0:Ycoordinate: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;
end
% 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. https://www.mathworks.com/help/matlab/ref/uieditfield.html
You can configure its ValueChangedFcn https://www.mathworks.com/help/matlab/ref/matlab.ui.control.numericeditfield-properties.html#buh_e24-76_sep_shared-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.
  18 Comments
Walter Roberson
Walter Roberson on 7 Aug 2021
Can i use the save function as .Xls file instead of .mat file ?
No. However, you could
sheetname = something appropriate
writetable(app.sensor_table.Data, 'app13.xls', 'sheet', sheetname);

Sign in to comment.

More Answers (1)

Jai Khurana
Jai Khurana on 2 Jul 2021
  2 Comments
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.

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!