Listbox showing variables with for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Christopher Wallace
le 22 Juil 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
le 22 Juil 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?
Réponses (2)
Christopher Wallace
le 23 Juil 2018
Modifié(e) : Christopher Wallace
le 23 Juil 2018
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));
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!