MATLAB Answers

How to plot a selected parameter off a list box?

10 views (last 30 days)
Chamath Vithanawasam
Chamath Vithanawasam on 30 Jul 2018
Edited: Chamath Vithanawasam on 31 Jul 2018
I am creating a GUI that collects data from a group of .txt files. These text files have multiple sets of values, where I only need one of them plotted at any given time. So the intention is to select a value, and then press the 'plot' button, and see that value get plotted.
There is a label to the right of the listbox that displays the name of the y-axis value which is selected. This is done using the following coding.
function GUI_for_log_OpeningFcn(hObject, eventdata, handles, varargin)
evalin('base','clear all');
evalin('base','clc');
listString = get(handles.listbox3,'String');
assignin('base','listString',listString);
the listbox code is as shown
function listbox3_Callback(hObject, eventdata, handles)
listValue = get(handles.listbox3,'Value');
assignin('base','listValue',listValue);
listString = evalin('base','listString');
set(handles.text4,'String',listString(listValue,1));
How do I manage to plot the y-axis values after selecting it from the list box? Currently the pushbutton will plot a hardcoded y-axis value, but I wish for that to be changed from the GUI.
function pushbutton1_Callback(hObject, eventdata, handles)
a = getappdata(0,'totData');
axes(handles.axes1);
plot((a{:,1}),(a{:,8}));
Where 'totData' is a table which holds all of the imported file data in one table. I am attaching two of the files where the raw data is obtained from.

  3 Comments

Jan
Jan on 30 Jul 2018
Poking around in the base workspace by assignin is a bad idea in general. See http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Especially the clear all is ugly. Using another workspace to collect a pile of data is a shot in your knee. Using the ApplicationData of the root object is the next method to access variables globally.
The idea is to keep all data inside the figure: Use the figure's ApplicationData or UserData, e.g. by using the guidata command. This is much easier, safer, cleaner, better to maintain and to debug.
The solution should be something like this:
Get the selection from the listbox:
value = get(handles.listbox3, 'Value');
str = get(handles.listbox3, 'String')
Selected = str{value};
How this can be used to select an Y-axis is unclear. You did not explain, how the strings in the list box are related to the data.
Chamath Vithanawasam
Chamath Vithanawasam on 31 Jul 2018
Well each of the strings in the list box is the name of one of the columns of data. The image below shows the combined table that is created when the GUI is initiated.
All the columns are recorded at specific time intervals. What I intend to see is when I click the desired string from the listbox the plot function plots the corresponding data on the graph, with the 'TimeStamp' on the x axis
Chamath Vithanawasam
Chamath Vithanawasam on 31 Jul 2018
I figured out how to do it, in my code the variable named 'listValue' holds the column number. All I had to do was get that value into the pushbutton callback as shown below.
function pushbutton1_Callback(hObject, eventdata, handles)
listValueAA = get(handles.listbox3,'Value');
assignin('base','listValueAA',listValueAA);
listString = evalin('base','listString');
set(handles.text4,'String',listString(listValueAA,1));
%disp(listValueAA+1);
a = getappdata(0,'totData');
axes(handles.axes1);
plot((a{:,1}),(a{:,listValueAA+1}));
Apologies for not asking the question clearly, I am very new to Matlab GUIDE. Like, 1 week new.

Sign in to comment.

Answers (0)

Sign in to answer this question.

Products


Release

R2017b