Listbox showing variables with for loop

조회 수: 2 (최근 30일)
Kostas Staios
Kostas Staios 2018년 7월 22일
댓글: Kostas Staios 2018년 7월 23일
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
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
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개)

Kostas Staios
Kostas Staios 2018년 7월 22일
편집: Kostas Staios 2018년 7월 22일
ΟΚ, 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 2018년 7월 23일
편집: Christopher Wallace 2018년 7월 23일
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
Kostas Staios
Kostas Staios 2018년 7월 23일
Thank you very much, that was really helpful to understand how it works :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by