Listbox showing variables with for loop
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Christopher Wallace
2018-7-22
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
2018-7-22
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?
回答(2 个)
Christopher Wallace
2018-7-23
编辑:Christopher Wallace
2018-7-23
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));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192395/image.png)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!