MATLAB Answers

0

Listbox showing variables with for loop

Asked by Kostas Staios on 22 Jul 2018
Latest activity Commented on by Kostas Staios on 23 Jul 2018
Hello, I have difficulties finding out a solution for my problem. So, the user is giving some data, which are some signals but the number of signals are not sure(may be 3 or even 15). So what I want to do is, after I get the variable with the number of signals(lets say the user puts 4 signals) and (I guess) with a for loop, show on my listbox:
signal 1
signal 2
signal 3
signal 4
and after the user selects one of this option(lets say he/she selects signal 3), I want to get some variable, which say that user choose signal 3. Is that possible? Did I make myself clear?

  2 Comments

What is the data type for the input data? Can you provide an sample data and the code you have tried so far?
You did not make yourself clear. You could use inputdlg() to have the user type in the number, or you could use menu() to have the user click a button with the number on it. Is that what you meant? What do you want to do with the signal after it's selected, or is that irrelevant?

Sign in to comment.

2 Answers

Answer by Kostas Staios on 22 Jul 2018
Edited by Kostas Staios on 22 Jul 2018

ΟΚ, so the user is inserting data as matrix (lets say a 2000x7 matrix), and every column is the signal response. So in the 2000x7 matrix, I have 7 signals; the signals can be more or less than 7. So, I want my listbox in GUI to show signal 1 till signal 7, and after the user choose lets say signal 3, to be able to plot only signal 3(the thrid column on my matrix). I havent done any code for that because I dont know how to make my GUI listbox that dynamic, showing that many variables with a for loop.

  0 Comments

Sign in to comment.


Answer by Christopher Wallace on 23 Jul 2018
Edited by Christopher Wallace on 23 Jul 2018

There are a few ways to create a GUI. For this example I'm using GUIDE
I added three objects, a listbox and two push buttons.
When the 'Load Data' push button is pressed it creates sample user data as you have described above and then populates the listbox with items going "Signal 1" to "Signal n" based on the number of columns in the sample data.
When the 'Plot Data' button is pressed it will plot the data in the selected signals column from the user data.
Here are the callbacks for the two pushbuttons.
% --- Executes on button press in loadUserData.
function loadUserData_Callback(hObject, eventdata, handles)
% hObject handle to loadUserData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.userData = randi(100, [2000,7]);
guidata(hObject, handles);
numOfSignals = size(handles.userData, 2);
signalNames = cell([1, numOfSignals]);
for i=1:numOfSignals
signalNames{1,i} = ['Signal ', num2str(i)];
end
set(handles.listbox, 'String', signalNames);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectedSignal = get(handles.listbox, 'Value');
plotFig = figure;
plot(handles.userData(:,selectedSignal));

  1 Comment

Thank you very much, that was really helpful to understand how it works :)

Sign in to comment.