Trouble plotting variables from cache menu and table

3 views (last 30 days)
I created a table and assigned some variables to that table. I then created a drop down menu and called each one X and Y. This way I could just plot X and Y and I thought it would plot the variable at X and the variable at Y. It did not seem to work.
I got the error:
Error using matlab.ui.control.internal.model.ExactlyOneSelectionStrategy/validateValuePresentInItems (line 183)
'Value' must be an element defined in the 'Items' property.
Error in matlab.ui.control.internal.model.SelectedTextValueStrategy/validateValue (line 38)
value = obj.Component.SelectionStrategy.validateValuePresentInItems(newValue);
Error in matlab.ui.control.internal.model.AbstractStateComponent/set.Value (line 141)
value = obj.ValueStrategy.validateValue(newValue);
I added this to my properties:
Thrust = table2array(app.t(:,"Thrust")); % Both of these needs to be an element defined in
Time = table2array(app.t(:,"Time")); % the items property
Then I got the error:
Invalid default value for property 'Thrust' in class 'Appdesigner Name':
Unable to resolve the name app.t
The code that gave me the first problem:
function PlotButtonPushed(app, event)
Thrust = table2array(app.t(:,"Thrust"));
Time = table2array(app.t(:,"Time"));
app.XDropDown.Value = Thrust;
app.YDropDown.Value = Time;
X = app.XDropDown.Value;
Y = app.YDropDown.Value;
plot(app.UIAxes, X, Y);
The values from the table need to be opened first and it seems there is some kind of problem there.
The error pops up when I try to switch the variables in the cache menu or plot them.
I would like to define my variables as the values from my table and be able to choose which variables I want to plot. The values are a column of numbers.
Also, I was able to plot the values directly from the table, but that is not really user friendly and I am trying to plot the values using a cache menu to make it more user friendly. I did that with the below code:
function PlotButtonPushed(app, event)
Thrust = table2array(app.t(:,"Thrust"));
Time = table2array(app.t(:,"Time"));
plot(app.UIAxes, Time, Thrust)

Answers (1)

Srijith Kasaragod
Srijith Kasaragod on 22 Sep 2021
Edited: Srijith Kasaragod on 23 Sep 2021
Hi,
I understand that you have a table consisting of multiple variables and you wish to have 2 dropdowns in the app, which selects variables from table to be plotted onto the UIAxes. Following is an example solution.
App contains two dropdowns, one button and a UIAxes. Three variables are declared to hold the table and dropdown values.
properties (Access = private)
t; %variable to hold the table
X; %variable to hold x-dropdown selection
Y; %variable to hold y-dropdown selection
end
Startup function reads and saves the table into variable 't'. Using this table, x and y dropdowns are initialised to have all the variable names so that user can select which variables to plot.
function startupFcn(app)
app.t= readtable("data.xlsx");
app.XDropDown.Items=app.t.Properties.VariableNames;
app.YDropDown.Items=app.t.Properties.VariableNames;
end
When dropdown values are changed, it is reflected in the variables 'X' and 'Y'.
% Value changed function: XDropDown
function XDropDownValueChanged(app, event)
value = app.XDropDown.Value;
app.X= value;
end
% Value changed function: YDropDown
function YDropDownValueChanged(app, event)
value = app.YDropDown.Value;
app.Y= value;
end
Once 'X' and 'Y' variables contain the values to be plotted, pushbutton callback function can plot them onto the UIAxes.
function PlotButtonPushed(app, event)
app.X= app.t{:,app.XDropDown.Value};
app.Y= app.t{:,app.YDropDown.Value};
plot(app.UIAxes,app.X,app.Y);
end
Please refer 'uidropdown' documentation to know more about callback functions and assigning items of uidropdown.

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!