look up data from table (either from excel, table in matlab, or text fil) using App Developer
4 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
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
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.)
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,
- You can add callback function to the dropdown itseld and the orbital eccentricity is update with every change in dropdown value (same with altitude)
- I have used madeup data in lookuptable.xlsx but I reckon this method should suffice or let me know if you encounter another problem
- 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
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!!
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
Intfcn = @(x,y) interp2(Xm,Ym,Zm,xvalue,yvalue,'nearest'); % Function Version
Int = Intfcn(xvalue,yvalue) % Function Call
.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!