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 个)

interp2 if you have a grid of inclination and altitude.
If you have lists of inclinations and corresponding altitudes, then use scatteredInterpolant

2 个评论

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);
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.)

请先登录,再进行评论。

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 个评论

Chayyank, this sounds like EXACTLY what I am trying to do. However, when I try to run this I get the following error: Error: File: Probofcoll.mlapp Line: 57 Column: 1
Illegal use of reserved keyword "classdef".
I tried to look this up, but am unable to really understand what the problem is.
I tried both running it in the command window and saving it in the app developer code. Also attached is the file excel file and the sample code (i edited yours a bit to match my inputs in my app I'm developing).
But yes, I would like the user to use the pull down in the app with predetermine altitude and inclinations and the code to look up that value and display (and eventually use that value to calculate the probability of collision - but baby steps).
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.
Walter, that was an easy fix. Thank you! So now i'm getting this:
>> test
Error using test/createComponents
'Value' must be an element defined in the 'Items' property.
Error in test (line 92)
createComponents(app)
>>
Line 92 is this from the above example:
% Create UIFigure and components
createComponents(app)
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.
Ah! That was it. Thank you both VERY much!! Now how to figure out how to insert this into my app. Wish me luck!!!
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!!
Chhayank - thanks so much for your help. Here's the trouble I'm having now.
I was able to make a standalone code that pulls data based on your original reply - still would not pull from the drop down menu. So I attempted to use portions of your example into what I already have, no dice. The layout is great, I'm starting with pulling the Flux data from the tool by the user inputting the Altitude and inclination by a drop down, and then it pulls that value from the table and when you change it and hit the "Flux" button it will pull that value.
Then the goal is to do the same for the cross sectional area, which has an equation of pi(r_t + r_i)^2 - and it will calculate when the user hits the "Cross Sectional Area" button. Same user will enter the "Lifetime" value in years, and check the box if it's a constellation. If it's "no", then this equation is used to calculate the probability of collision
N = F*A_c*T
where F is the Flux, A_c is the cross sectional area the T is the lifetime. User will hit the "Calculate the Prob. of Collision" button and viola, for just one satellite.
If the user selected "yes" for the constellation question, and then the equation is
N_c = sum(F*A_c*T)
Attached is my spreadsheet, so you can see it's not jsut an x and y value, it's a whole table of quite a few values. Also attached is where I'm at with my .mapp file. I'm stuck at the first part of the calculating the flux. Just giving you my whole scenario for context - really looking for help on the drop down part to figure out how to use the App designer as it preinputs all kinds of the code already - which is grayed out and then you put callbacks for the functions you specifically want to run.
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!!!
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
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