How to assign user entered values(integers entered in an edit text box) to an already user entered string in a listbox of MATLAB GUI? . Your support would be very useful.

3 ビュー (過去 30 日間)
Data Inputed by User: A String in the listbox of a GUI and 3 integer values in an edit text box of the same GUI. Every time the user should be entering a string and then 3 integer values (x,y & z coords). Now with the user entered 3 coordinates,I would like to plot the point and assign these values to the already entered string in the listbox. I face problems in assigning the three points to the string. Because as future scope, If I select the string in the listbox and click a delete button the corresponding point in the plot should get deleted. This whole process must function in a loop as every time the user would like to enter new strings and assign 3 coordinate values to it. I have been trying a lot but not able to achieve the output. Your help will be deeply appreciated.
  2 件のコメント
Brian B
Brian B 2014 年 7 月 15 日
What is your approach? What errors are you getting?
I would probably define a structure array with fields for the string, the x,y,z values, and a handle to the point. Then the "Add" and "Delete" buttons (I am assuming there is an "Add" button) would add an elements to or remove an element from this struct array.
matlablearner
matlablearner 2014 年 7 月 15 日
編集済み: matlablearner 2014 年 7 月 15 日
I tried something similar to what you have mentioned but I am struck at creating the structure and accessing it. I have attached the snapshot of my code for your reference.

サインインしてコメントする。

回答 (2 件)

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014 年 7 月 15 日
Let's say that h_list, and h_text, are the handles to your listbox and textbox, respectively (which you have already created). You could simply define the callback property of your textbox:
set(h_text,'callback',@(varargin)mycallbackfcn(h_list));
and then define this callback function to modify the strings within the listbox. Something along these lines:
function mycallbackfcn(h_list)
str = get(h_list,'string'); % current strings in listbox
val = get(h_list,'value'); % selected string in listbox
coords = get(gcbo,'string'); % text entered in textbox
str{val} = coords;
set(h_list,'string',str);
end

Brian B
Brian B 2014 年 7 月 16 日
編集済み: Brian B 2014 年 7 月 16 日
You are creating a struct correctly. However, you should also get the handle to the point when you create it:
h = scatter3(...);
and save the handle with the structure:
node = struct(..., 'nodehandle', h);
From the syntax highlighting I can tell that node is a persistent or global or shared variable; furthermore, the xc field of node is a vector with the x-coordinates of all of the nodes. It will probably be easier to define a struct ARRAY (call it Nodes, for example) as a persistent variable, and to let each element of the struct array represent one node. (In this case, you would get xc as xc = str2double(...) and not append it to any list; just save the scalar xc in node). Then you would create node as you have done and append it to Nodes:
Nodes(end+1) = node;
When the user selects a point, search through Nodes to find which element of the struct array has the matching handle and remove it from the list.
  2 件のコメント
matlablearner
matlablearner 2014 年 7 月 17 日
I tried your first suggestion by declaring node as a global variable and adding a handle to the point. But the problem is, * the node name and the co-ordinates are not connected. * SO when i select a node name and press delete button, only the name gets deleted, but the entered coordinates are still getting plotted.
%%Plot node button press
case 'plotnode'
% Set Default View
az = wrapTo360(-37.5); el = 30; %limits for az 0...360 for el 0...90
view(handles.axes1,[az el]);
set(handles.horizontalslider,'value',az/360);
set(handles.verticalslider,'value',el/90);
%Plot the points
hold on;
xc=str2double(get(handles.xcoord,'String'));
xc=[xc;str2double(get(handles.xcoord,'String'))];
yc=str2double(get(handles.ycoord,'String'));
yc=[yc;str2double(get(handles.ycoord,'String'))];
zc=str2double(get(handles.zcoord,'String'));
zc=[zc;str2double(get(handles.zcoord,'String'))];
h=scatter3(handles.axes1,xc,yc,zc,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[1 0 0]);
h=[h;scatter3(handles.axes1,xc,yc,zc,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[1 0 0])];
node=struct('nodenumber',nodenum,'xcoordinate',xc,...
'ycoordinate',yc,'zcoordinate',zc,'nodehandle',h);
%%Executes on Delete button press in Node Section
case 'deletenode'
nnames = get(handles.nodeslistbox,'String');
if length(nnames)<1;
return;
end; %if already empty, do nothing
index_selec = get(handles.nodeslistbox,'Value');
node.nodenum(index_selec)=[];
node.h(index_selec)=[];
val=index_selec-1;
if val<1; val=1;end %take care of exception
set(handles.nodeslistbox,'String',node.nodenum,'Value',val);
Brian B
Brian B 2014 年 7 月 18 日
編集済み: Brian B 2014 年 7 月 18 日
This would be easier if you plotted each point as a separate line.
case 'plotnode'
% Set Default View
az = wrapTo360(-37.5); el = 30; %limits for az 0...360 for el 0...90
view(handles.axes1,[az el]);
set(handles.horizontalslider,'value',az/360);
set(handles.verticalslider,'value',el/90);
%Plot the points
xc=str2double(get(handles.xcoord,'String'));
yc=str2double(get(handles.ycoord,'String'));
zc=str2double(get(handles.zcoord,'String'));
hold on;
h=scatter3(handles.axes1,xc,yc,zc,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[1 0 0]);
node=struct('nodenumber', [node.nodenum; nodenum],...
'xcoordinate', [node.xc; xc], ...
'ycoordinate', [node.yc; yc], ...
'zcoordinate', [node.zc; zc], ...
'nodehandle', [node.h; h]);
Before you delete the corresponding handle in node.h, delete the point:
case 'deletenode'
nnames = get(handles.nodeslistbox,'String');
if length(nnames)<1;
return;
end; %if already empty, do nothing
index_selec = get(handles.nodeslistbox,'Value');
node.nodenum(index_selec)=[];
delete(node.h(index_selec)); % THIS IS THE KEY!
node.h(index_selec)=[];
val=index_selec-1;
if val<1; val=1;end %take care of exception
set(handles.nodeslistbox,'String',node.nodenum,'Value',val);
Alternatively, you could remove the corresponding coordinates from node.xc etc and replot.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by