look up data from table (either from excel, table in matlab, or text fil) using App Developer
显示 更早的评论
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?
回答(3 个)
Walter Roberson
2023-8-14
0 个投票
interp2 if you have a grid of inclination and altitude.
If you have lists of inclinations and corresponding altitudes, then use scatteredInterpolant
2 个评论
Veronica Vigil
2023-8-14
编辑:Walter Roberson
2023-8-14
Walter Roberson
2023-8-14
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
2023-8-14
编辑:Walter Roberson
2023-8-18
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 个评论
Veronica Vigil
2023-8-18
Walter Roberson
2023-8-18
The line starting from
%An example what the complete code might look like
should go into a file named test.m
The Samplecode.txt that you posted is something that came from an editor with editor markup, and is not something that could itself be executed -- the code would have to be extracted from the file to execute it.
Veronica Vigil
2023-8-18
Walter Roberson
2023-8-18
You define app.AltitudekmDropDown.Items with entries starting from '300' but you have app.AltitudekmDropDown.Value = '1'; but '1' does not appear anywhere on the entry list.
Veronica Vigil
2023-8-18
Chhayank Srivastava
2023-8-19
You are welcome, the code posted was taken from matlab's app designer with app name test.m
function app = test
apologies if this caused any confusion.
But, you can take inspiration from above code and paste your modified code in the de-highlighted
region generated by the app designer.
Let us know if we could help in any other problem!!
Always, Happy to help!!

Veronica Vigil
2023-8-19
Veronica Vigil
2023-8-19
编辑:Walter Roberson
2023-8-21
Chhayank Srivastava
2023-8-21
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!!
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
.
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!