Trouble plotting variables from cache menu and table
3 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
Srijith Kasaragod
2021-9-22
编辑:Srijith Kasaragod
2021-9-23
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!