Listbox showing variables with for loop

2 Ansichten (letzte 30 Tage)
Kostas Staios
Kostas Staios am 22 Jul. 2018
Kommentiert: Kostas Staios am 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 Kommentare
Christopher Wallace
Christopher Wallace am 22 Jul. 2018
What is the data type for the input data? Can you provide an sample data and the code you have tried so far?
Image Analyst
Image Analyst am 22 Jul. 2018
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?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Kostas Staios
Kostas Staios am 22 Jul. 2018
Bearbeitet: Kostas Staios am 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.

Christopher Wallace
Christopher Wallace am 23 Jul. 2018
Bearbeitet: Christopher Wallace am 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 Kommentar
Kostas Staios
Kostas Staios am 23 Jul. 2018
Thank you very much, that was really helpful to understand how it works :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by