Problem with quiver plot function

3 views (last 30 days)
Penny
Penny on 21 Sep 2018
Commented: Adam on 24 Sep 2018
I am working on some code which I have been trying to tidy up into functions as I have several versions of it and it's quite repetitive. One of the tasks is a quiver plot, which I have written this function (Qplot) for:
function Qplot( space, lat, lon, u, v, size, thick )
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
hold on
r = 1:str2double(space):length(lat);
r1 = lat(r);
s = 1:str2double(space):length(lon);
s1 = lon(s);
us = u(r, s);
vs = v(r, s);
q = quiver(s1,r1,us,vs);
q.AutoScaleFactor = str2double(size);
q.LineWidth = str2double(thick);
q.Color = 'k';
hold off
end
This works fine on most versions of the code except one. I have had to take out the Qplot bit out (it's there commented out) and replace it with the content of the function in full, like this:
function pushbutton23_Callback(hObject, eventdata, handles) %#ok<*INUSL>
h = guidata(gcbo);
axes(handles.axes2)
% Qplot(handles.edit22.String, h.latf, h.lonf, h.uf. h.vf, handles.edit21.String, handles.edit23.String)
hold on
r = 1:str2double(handles.edit22.String):length(h.latf);
r1 = h.latf(r);
s = 1:str2double(handles.edit22.String):length(h.lonf);
s1 = h.lonf(s);
us = h.uf(r, s);
vs = h.vf(r, s);
q = quiver(s1,r1,us,vs);
q.AutoScaleFactor = str2double(handles.edit21.String);
q.LineWidth = str2double(handles.edit23.String);
q.Color = 'k';
hold off
If I use the Qplot function in that one version only, I get this error message, but in no others where I'm doing exactly the same thing:
Struct contents reference from a non-struct array object.
Error in guiglobal>pushbutton23_Callback (line 280)
Qplot(handles.edit22.String, h.latf, h.lonf, h.uf. h.vf, handles.edit21.String, handles.edit23.String)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in guiglobal (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)guiglobal('pushbutton23_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Any ideas?
  10 Comments
Penny
Penny on 24 Sep 2018
Handles only covers the axes, that line is needed because I also need to access variables in the h. structure to tell it what to plot.
What might it not do as expected? Turns out that's not the problem here, there was a typo as spotted below, but I am interested in what that does which my current code does not. It seems to robustly always plot to the correct axes regardless of what else I've been doing.
Adam
Adam on 24 Sep 2018
h = guidata(gcbo);
gives you the handles struct. The exact same one that you get passed into the function as 'handles'.
Generally code using
axes( obj.hAxes )
followed by a load of plot related instructions will work fine, but from time to time you will find a case where the 'current axes' changed without you realising between you calling axes and actually plotting data. I have had numerous occasions when I forgot to give explicit axes handles when I suddenly get an axes appearing in a figure I wasn't expecting it.
So sure, it may work in all cases you have had so far, but it is just good practice to give explicit handles to plot instructions rather than just hope that nothing unexpectedly changes the current axes before you plot (e.g. a user clicking on a different axes will change the current axes too).

Sign in to comment.

Accepted Answer

Dennis
Dennis on 24 Sep 2018
You got a typo:
Qplot(handles.edit22.String, h.latf, h.lonf, h.uf. h.vf, handles.edit21.String, handles.edit23.String)
^^ this should be a ,

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!