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

2 次查看(过去 30 天)
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
Walter Roberson 2023-8-14
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
Veronica Vigil 2023-8-14
编辑:Walter Roberson 2023-8-14
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 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
编辑: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,
  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 个评论
Veronica Vigil
Veronica Vigil 2023-8-19
编辑:Walter Roberson 2023-8-21
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
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
Star Strider 2023-8-21
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
.

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by