How to create 3d mesh plot in GUI?

4 views (last 30 days)
Ksenia
Ksenia on 25 Sep 2014
Commented: Geoff Hayes on 26 Sep 2014
Hi, guys! I'm creating GUI with some representation of my mathematical model. I need to print out several graphics: 2D and 3D. My 2D plot of (x,y) works fine, but 3D mesh doesn't show up... Here's code:
For my 2D plot:
function PlotHeavisideInsects_Callback(hObject, eventdata, handles)
InsectN1 = str2double(get(handles.varN1, 'String'));
InsectN2 = str2double(get(handles.varN2, 'String'));
InsectX1 = str2double(get(handles.varX1, 'String'));
InsectX2 = str2double(get(handles.varX2, 'String'));
InsectX3 = str2double(get(handles.varX3, 'String'));
InsectX4 = str2double(get(handles.varX4, 'String'));
x = 0:1200;
y = InsectN1*(heaviside(x-InsectX1)-heaviside(x-InsectX2))+InsectN2*(heaviside(x-InsectX3)-heaviside(x-InsectX4));
plot(x,y)
For my 3D plot:
function btnPlot_Callback(hObject, eventdata, handles)
InsectD = str2double(get(handles.varD, 'String'));
Insectv = str2double(get(handles.varv, 'String'));
[a,b] = meshgrid(0:5:1200, 0:5:1200);
c = (InsectN1/2)*(erf((x-InsectX1)./(2*sqrt(InsectD*Insectv*y)))-erf((x-InsectX2)./(2*sqrt(InsectD*Insectv*y))))+...
(InsectN2/2)*(erf((x-InsectX3)./(2*sqrt(InsectD*Insectv*y)))-erf((x-InsectX4)./(2*sqrt(InsectD*Insectv*y))));
mesh(a,b,c)
Maybe function of 3D doesn't recognize parameters from function 2D?

Answers (1)

Geoff Hayes
Geoff Hayes on 25 Sep 2014
Ksenia - you're correct in saying that the 3D function callback doesn't recognize the x and y (or any other) variables from the 2D callback. You must be observing some sort of error along the lines of Undefined function or variable 'x' or Undefined function or variable 'y' when you press the second button.
If you want the 3D callback to use variables that have been initialized in another callback, then save those variables to the handles structure using the guidata function.
Modify your PlotHeavisideInsects_Callback callback as follows
function PlotHeavisideInsects_Callback(hObject, eventdata, handles)
handles.InsectN1 = str2double(get(handles.varN1, 'String'));
handles.InsectN2 = str2double(get(handles.varN2, 'String'));
handles.InsectX1 = str2double(get(handles.varX1, 'String'));
handles.InsectX2 = str2double(get(handles.varX2, 'String'));
handles.InsectX3 = str2double(get(handles.varX3, 'String'));
handles.InsectX4 = str2double(get(handles.varX4, 'String'));
handles.x = 0:1200;
handles.y = handles.InsectN1*(heaviside(handles.x-handles.InsectX1)-heaviside(handles.x -...
handles.InsectX2))+handles.InsectN2*(heaviside(handles.x-handles.InsectX3) - ...
heaviside(handles.x-handles.InsectX4));
plot(handles.x,handles.y)
% save the data to the structure so that it is accessible in other callbacks
guidata(hObject,handles);
Then, in your btnPlot_Callback access the above data through the handles structure as
function btnPlot_Callback(hObject, eventdata, handles)
InsectD = str2double(get(handles.varD, 'String'));
Insectv = str2double(get(handles.varv, 'String'));
[a,b] = meshgrid(0:5:1200, 0:5:1200);
% ensure that at least the InsectN1 field exists in the structure
if isfield(handles,'InsectN1')
c = (handles.InsectN1/2)*(erf((handles.x-handles.InsectX1)./(2*sqrt(...
handles.InsectD*handles.Insectv*handles.y)))-erf((handles.x - ...
handles.InsectX2)./(2*sqrt(handles.InsectD*handles.Insectv*handles.y))))+...
(handles.InsectN2/2)*(erf((handles.x-handles.InsectX3)./(2*sqrt(...
handles.InsectD*handles.Insectv*handles.y)))-erf((handles.x - ...
handles.InsectX4)./(2*sqrt(handles.InsectD*handles.Insectv*handles.y))));
mesh(a,b,c)
end
The code is the same as before except that all variables created in the other callback that we wish to access from here are prefixed with handles.. Note how we have a check to make sure that at least the InsectN1 field exists in the handles structure before we do any work (to avoid any errors such as Reference to non-existent field 'x').
  2 Comments
Ksenia
Ksenia on 26 Sep 2014
Thanx, Geoff for your response! I'm totally understand my mistake, but my 3D graphic doesn't show up anyway =(( Moreover, there are no mistakes in command window. So I'm confused... Is there any difference in representing 2D and 3D? I mean there is only one method called "axes" to built a graphic?
I'm a newby to the Matlab, but I really need to master it due to my future Master degree ;)
Geoff Hayes
Geoff Hayes on 26 Sep 2014
In your GUI, do you just have one or two axes for your plots? Unless you renamed them, the first will have the tag/name axes1, the second axes2, etc. You can explicitly set the axes that you want to draw your 3D graphic on as
mesh(handles.axes1,a,b,c);
You may also want to put a breakpoint at the line
mesh(handles.axes1,a,b,c);
and then re-run your application. Once you press the button to display the 3D data, the code will pause in the debugger/editor at this line. What can you say about a, b, and c? The first two variables should obviously be set correctly because of
[a,b] = meshgrid(0:5:1200, 0:5:1200);
but what about c? Look at its size/dimensions, some of its values. What do you see?

Sign in to comment.

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!