look up data from table (either from excel, table in matlab, or text fil) using App Developer

3 views (last 30 days)
Hello! I'm developing an app in the App developer, and the first step is I need to look up a value in a table based on the users input from 2 drop down menus (think x and y). How do I do this? Right now, I have it set up so the user has to pick an Inclination (x in excel table) and Altitude (y in excel table), i then want them to hit a button, and the code will look up that value and display it in a numeric edit field. How do I do this?

Answers (3)

Walter Roberson
Walter Roberson on 14 Aug 2023
interp2 if you have a grid of inclination and altitude.
If you have lists of inclinations and corresponding altitudes, then use scatteredInterpolant
  2 Comments
Veronica Vigil
Veronica Vigil on 14 Aug 2023
Edited: Walter Roberson on 14 Aug 2023
Hey Walter, thanks for the quick reply. So I'm a total noob, and don't really know how to implement that. When I click on the two links provided it doesn't tell me how to write the code exactly. Again, I'm sorry I'm very new at this. I'm specifically trying to just pull the data to a corresponding x and y and display it in the app designer tool. I have the following code, but it doesn't do anything, nor give me an error:
value = app.EditField_2.Value; ///this is where I want the value displayed
% Import the data from Excel for a lookup table
Flux = readtable(FluxData,'Sheet','Sheet1'); //this is the excel table I'm pulling from; it is loaded into the file in matlab
% Row indices for lookup table
Inlination = Flux(3:end,1)';
% Column indices for lookup table
Altitude = Flux(1,3:end);
% Output values for lookup table
app.EditField_2 = Flux(app.InclinationDegDropDown,app.AltitudekmDropDown:end);
Walter Roberson
Walter Roberson on 14 Aug 2023
You are using readtable(), but you are using rows 3:end and columns 3:end which suggests strongly that what you want is to read a numeric matrix (possibly skipping the first line or two.)

Sign in to comment.


Chhayank Srivastava
Chhayank Srivastava on 14 Aug 2023
Edited: Walter Roberson on 18 Aug 2023
Hi,
You can start by importing the lookup sheet (.xlsx) which has your x & y values. ( https://www.mathworks.com/help/matlab/ref/readtable.html )
Table = readtable('<filename>.xls','ReadRowNames',true);
This line imports your data into table Table. Assuming the first column is x (Inclination) and the second column is y (Altitude) and another assumption from my side is you want to lookup some corresponding value say orbital eccentricity(just taken as an example) on column three, your next line would look like
x = Table(:,1);
y = Table(:,2);
z = Table(:,3);
Now you can using the callback command and subsequently a find command find z cooresponding to x & y.
Another assumption here is that you know values you are going to offer the user in the dropdown menu (alternatively you can use x & y from above to fill the dropdown menu)
Since I don't have the data I used the below attached excel file (lookuptable.xlsx) as my lookup table which then the program reads and uses as lookup table.
%An example what the complete code might look like
classdef test < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
OrbitaleccentricityEditField matlab.ui.control.NumericEditField
OrbitaleccentricityEditField_2Label matlab.ui.control.Label
RunButton matlab.ui.control.Button
AltitudeDropDown matlab.ui.control.DropDown
AltitudeDropDownLabel matlab.ui.control.Label
InclinationDropDown matlab.ui.control.DropDown
InclinationDropDownLabel matlab.ui.control.Label
end
properties (Access = private)
orbital % Description
altitude
inclination
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
Table = readtable('lookuptable.xlsx','ReadRowNames',false);
app.inclination = Table{:,1};
app.altitude = Table{:,2};
app.orbital = Table{:,3};
app.OrbitaleccentricityEditField.Value = app.orbital(1);
end
% Button pushed function: RunButton
function InclinationDropDownValueChanged(app, event)
xvalue = app.InclinationDropDown.Value;
yvalue = app.AltitudeDropDown.Value;
xindices = find(app.inclination==str2double(xvalue));
yindices = find(app.altitude==str2double(yvalue));
idx=intersect(xindices,yindices);
app.OrbitaleccentricityEditField.Value = app.orbital(idx);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create InclinationDropDownLabel
app.InclinationDropDownLabel = uilabel(app.UIFigure);
app.InclinationDropDownLabel.HorizontalAlignment = 'right';
app.InclinationDropDownLabel.Position = [55 355 59 22];
app.InclinationDropDownLabel.Text = 'Inclination';
% Create InclinationDropDown
app.InclinationDropDown = uidropdown(app.UIFigure);
app.InclinationDropDown.Items = {'1', '2', '3', '4', '5', '6'};
app.InclinationDropDown.Position = [129 355 100 22];
app.InclinationDropDown.Value = '1';
% Create AltitudeDropDownLabel
app.AltitudeDropDownLabel = uilabel(app.UIFigure);
app.AltitudeDropDownLabel.HorizontalAlignment = 'right';
app.AltitudeDropDownLabel.Position = [408 355 46 22];
app.AltitudeDropDownLabel.Text = 'Altitude';
% Create AltitudeDropDown
app.AltitudeDropDown = uidropdown(app.UIFigure);
app.AltitudeDropDown.Items = {'1', '2', '3', '4', '5'};
app.AltitudeDropDown.Position = [469 355 100 22];
app.AltitudeDropDown.Value = '1';
% Create RunButton
app.RunButton = uibutton(app.UIFigure, 'push');
app.RunButton.ButtonPushedFcn = createCallbackFcn(app, @InclinationDropDownValueChanged, true);
app.RunButton.Position = [495 221 100 22];
app.RunButton.Text = 'Run';
% Create OrbitaleccentricityEditField_2Label
app.OrbitaleccentricityEditField_2Label = uilabel(app.UIFigure);
app.OrbitaleccentricityEditField_2Label.HorizontalAlignment = 'right';
app.OrbitaleccentricityEditField_2Label.Position = [190 137 104 22];
app.OrbitaleccentricityEditField_2Label.Text = 'Orbital eccentricity';
% Create OrbitaleccentricityEditField
app.OrbitaleccentricityEditField = uieditfield(app.UIFigure, 'numeric');
app.OrbitaleccentricityEditField.Position = [309 137 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = test
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Few points to note over here,
  1. You can add callback function to the dropdown itseld and the orbital eccentricity is update with every change in dropdown value (same with altitude)
  2. I have used madeup data in lookuptable.xlsx but I reckon this method should suffice or let me know if you encounter another problem
  3. I have assumed Altitude and inclination are independent parameters (if not this can be tweaked easily in the above code)
Let me know if this works for you!!
Thank you
  9 Comments
Veronica Vigil
Veronica Vigil on 19 Aug 2023
Edited: Walter Roberson on 21 Aug 2023
Okay! I literally have EVERYTHING working, Except for the table look up part. I can calcute N based on user inputs for lifetime and Cross Sectional area. The whole tool works if I manually enter in the flux.
Right now, this is what I have for the table look up, which is obviously wrong:
% Callback function: DisplayFlux, FluxButton,
% InclinationDegDropDown
function FluxDataPulled(app, event)
Table = readtable('FluxDataInterpolated.xlsx','ReadRowNames',false);
app.Inclination = Table{:,1};
app.altitude = Table{:,2};
app.Flux = Table{:,3};
app.DisplayFlux.Value = app.Flux(1);
T1 = readtable('FluxDataInterpolated.xlsx')
xvar = T1{1,3:end};
yvar = T1{2:end,2};
xvar = app.InclinationDegDropDown.Value;
yvar = app.AltitudekmDropDown.Value;
xindices = find(app.InclinationDegDropDown==str2double(xvar));
yindices = find(app.AltitudekmDropDown==str2double(yvar));
A = intersect(xindices,yindices);
% xvar = T1{1,3:end};
% yvar = T1{2:end,2};
% A = T1{2:end,3:end};
end
I'm SO close! my .mlapp file and spreadsheet are in previous comment. Please and thank you!!!
Chhayank Srivastava
Chhayank Srivastava on 21 Aug 2023
Yes you are so close.
Try using
app.AltitudekmDropDown.Items = Table(:,1);
app.InclinationDegDropDown.Items = Table{:,1};
app.AltitudekmDropDown.Items = Table{:,2};
Above lines of code with take values from your xlsx file and populate the drop down.
Now coming to
app.Flux = Table{:,3};
app.DisplayFlux.Value = app.Flux(1);
I am referring to the above mlapp code you uploaded. Flux is just a button pressing which a callback function will be called upon. Now you when that function is called and after your calculation you can save the inteded value in app.EditField_2.Value variable which shall update Flux column
Also when you do above both things you dont need to update Items value as was done in your mlapp code.
Hope this solves it!!

Sign in to comment.


Star Strider
Star Strider on 21 Aug 2023
Another option for the lookup —
Table = readtable('FluxDataInterpolated.xlsx');
inclination = Table{1,2:end};
altitude = Table{2:end,1};
xvalue = 10;
yvalue = 300;
[Xm,Ym] = meshgrid(inclination,altitude);
Zm = Table{2:end, 2:end};
Int = interp2(Xm,Ym,Zm,xvalue,yvalue,'nearest') % Original Version
Int = 3.4780e-08
Intfcn = @(x,y) interp2(Xm,Ym,Zm,xvalue,yvalue,'nearest'); % Function Version
Int = Intfcn(xvalue,yvalue) % Function Call
Int = 3.4780e-08
.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!