Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Show figure

Asked by Cruise on 24 Nov 2011

Hi everybody.

I have a GUI to plot a 3D graph.My problem is graph showed in GUI.I want to show graph in separate figure.Thank all advice. It's my code callback function of pushbutton.

    function pushbutton1_Callback(hObject, eventdata, handles)
  % hObject    handle to pushbutton1 (see GCBO)
  % eventdata  reserved - to be defined in a future version of MATLAB
  % handles    structure with handles and user data (see GUIDATA)
  a1=str2num(get(handles.x1,'String'));
  b1=str2num(get(handles.y1,'String'));
  c1=str2num(get(handles.z1,'String'));
  a2=str2num(get(handles.x2,'String'));
  b2=str2num(get(handles.y2,'String'));
  c2=str2num(get(handles.z2,'String'));
  a3=str2num(get(handles.x3,'String'));
  b3=str2num(get(handles.y3,'String'));
  c3=str2num(get(handles.z3,'String'));
  plot3 ([a1 a2 a3],[b1 b2 b3],[c1 c2 c3],'Marker','o','LineStyle','-');
  grid on
  rotate3d on

0 Comments

Cruise

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 24 Nov 2011
Accepted answer
    function pushbutton1_Callback(hObject, eventdata, handles)
  % hObject    handle to pushbutton1 (see GCBO)
  % eventdata  reserved - to be defined in a future version of MATLAB
  % handles    structure with handles and user data (see GUIDATA)
  a1=str2num(get(handles.x1,'String'));
  b1=str2num(get(handles.y1,'String'));
  c1=str2num(get(handles.z1,'String'));
  a2=str2num(get(handles.x2,'String'));
  b2=str2num(get(handles.y2,'String'));
  c2=str2num(get(handles.z2,'String'));
  a3=str2num(get(handles.x3,'String'));
  b3=str2num(get(handles.y3,'String'));
  c3=str2num(get(handles.z3,'String'));
  f = figure();
  ax = axes('Parent',f);  %corrected from my original version
  plot3 (ax, [a1 a2 a3],[b1 b2 b3],[c1 c2 c3], 'Marker','o','LineStyle','-');
  grid(ax, 'on')
  rotate3d(ax, 'on')

3 Comments

Walter Roberson on 25 Nov 2011

You can insert the figure(2) call as Ricky shows, and that will probably look okay for now.

You will find with experience, though, that it is better to explicitly parent all graphics operations against the appropriate containing graphics object. If you do that, you will always be sure where the graphics are going to show up. If you do not do explicit parenting, then if you call a routine that does graphics somewhere else, then MATLAB's idea of what the "current" figure or axes is likely to change, and it can be frustrating to figure out why graphics seems not to appear or appears in completely the wrong place. The same thing can happen if you have a drawnow() or pause() or figure() command, because all of those give time for graphics interrupts that might incidentally change MATLAB's idea of "current" objects.

Another frustrating problem that can occur if you do not explicitly draw against the appropriate containing object, is that your code might draw in the right place when you run it right through, and yet might draw incorrectly when you go to debug it. This can happen because when you are stopped in the debugger, if you click on an object (e.g., to drag it out of the way so you can see the code window), that object will become the "current" object -- something your code is probably not designed to expect.

Have a look at the code in my Answer and notice that I pass the axes handle to plot3() and grid() and rotate3d(). When your code is written like that, then no matter what the "current" object gets inadvertently changed to, the graphics will go in the right place.

Walter Roberson on 25 Nov 2011

Cruise, try instead

ax = axes('Parent',f)
Cruise on 25 Nov 2011

It's OK. Thanks a lot.

Walter Roberson
Answer by Ricky on 24 Nov 2011
Edited by Randy Souza on 15 Aug 2012

You can insert a command figure in between the graph that you plot like this:

a1=str2num(get(handles.x1,'String'));
figure(1);
b1=str2num(get(handles.y1,'String')); 
figure(2);
...

Is that what you are after?

1 Comment

Cruise on 25 Nov 2011

Thank you so much!

Ricky

Contact us