how to work with the listbox?
6 ビュー (過去 30 日間)
古いコメントを表示
I hope I'll be clear in my question, what I want is as you see below in the image that I selected layer 1 in the listbox and I've entered different values in the table beside, now what I want that when I select layer 2 in the listbox, the table beside will get blank so that I can enter new values for layer 2 and etc.... for all layers knowing that when I have for example 3 layers and I go back to layer 1, I'll have the numbers which I have entered in this layer saved. I hope you can help me because I'm having a difficulty in it below you can see the image to understand better my question
回答 (2 件)
Michael Haderlein
2014 年 7 月 22 日
You need to use the callback function. A simple short example is the following (Copy this into a new m-file.):
function test
figure
htxt=uicontrol('style','edit','position',[200 80 80 20]);
hlist=uicontrol('style','list','position',[100 80 80 100],'string',{'A';'B';'C'},'Callback',@refresh);
vals=zeros(3,1);
lastind=1;
function refresh(src,evt)
vals(lastind)=str2double(get(htxt,'string'));
lastind=get(src,'value');
set(htxt,'string',num2str(vals(lastind)))
end
end
Best regards, Michael
2 件のコメント
Michael Haderlein
2014 年 7 月 22 日
I don't really get the problem right now. You just need to define the callback function for the list box. You can also do this in guide (right click on the list box). Of course you also need the handles of the edit boxes, but you seem to already have them. In my solution, the default value (0) will be set to unmodified edit boxes. If you rather want them to be empty, you'd just need to set it to an empty string instead.
Geoff Hayes
2014 年 7 月 22 日
編集済み: Geoff Hayes
2014 年 7 月 22 日
What you can do is save layer data (so that layer specific data which is defined by the variables to the right of your GUI) to the handles structure each time the user selects an item in your list box.
Suppose that the listbox is named listbox1, then the callback for this listbox (as created through GUIDE) can be something like the following
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as
% cell array contents{get(hObject,'Value')} returns selected item from
% listbox1
% get the currently selected layer (i.e. that which the user just selected)
currLayer=get(hObject,'Value');
% layer data is saved to a field in the handles structure - if no such field
% exists then we have to create it as an empty array
if ~isfield(handles,'layerData')
handles.layerData = [];
end
% ensure that given the currently selected layer ensure that there is one layer
% data struct per layer
while currLayer>length(handles.layerData)
% add the structure with fields that match the table with values defaulted to
% zero
handles.layerData = [handles.layerData ; ...
struct('E',0,'X',0,'Nd',0,'Na',0,'Nc',0,'Nv',0,'mun',0,'mup',0,'eps',0)];
end
% we need to save the data from the table to the previously selected layer, if
% it exists (again we use a field in the handles structure)
if isfield(handles,'prevLayer')
prevLayer = handles.prevLayer;
if prevLayer>0 && prevLayer<=length(handles.layerData)
% you have to write this code, populating the structure for that
% previously selected layer, given your edit text widgets in the
% table
% handles.layerData(prevLayer).E = E text data
% handles.layerData(prevLayer).X = X text data
% etc.
end
end
% set the previous layer selection to the currently selected layer for future
% reference
handles.prevLayer = currLayer;
% copy the currently selected layer data to the edit text boxes in the table
% you have to write the code to populate your edit text widgets in the table
% given the handles.layerData(currData) structure
% save the data to the handles structure so that it is available in all other
% callbacks
guidata(hObject,handles);
Try the above and see what happens!
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!