I have been trying to create a MATLAB GUI using Guide which :
i) has a function that takes in input parameters (Temperature, Superconducting Gap Energy, Broadening Parameter) and outputs a plot - a Density of States plot showing a superconducting energy gap.
Note: This function involves numerical double integration, so the parameters can not be varied in a straightforward manner by using sliders. To study the variation in the curve from different parameters, each time the new parameters are input, the function double integrates again and a new curve is generated within the same axes.
ii) The GUI has 4 sets of input fields so that 4 curves can be plotted within the same axes at at a given time for comparison.
I used the following code:
handles.P1=plot(handles.dIdV,bias1,cond1,'r','linewidth',2);
%P1 = plot(handles.dIdV,bias1,cond1,'r','linewidth',2);
hold on
%plot(x,data(:,2),'b','linewidth',2)
%scatter(E,XDOS)
%xlim([min(bias) max(bias)])
%title(nem)
(Kindly do not get confused by the comments. 'cond1' is the result of integration for set1 of parameters, this is plotted against the array 'bias'. Thus the DOS (function of bias, temperature, delta-gap value, etc) is integrated up to each 'bias' value and stored in 'cond1', which is then plotted against that 'bias' value. )
to clear this, I wrote:
% --- Executes on button press in clear1.
function clear1_Callback(hObject, eventdata, handles)
% hObject handle to clear1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.temp1=0;
handles.delta1=0;
handles.tao1=0;
handles.vmod1=0;
handles.val1=0;
hold off
i.e. all the parameters are reset to zero and hold function is turned off.
The same bit of code has been written 4 times for 4 different curves.
So each curve has a hold function when the plot command is given and when the clear button is pressed, the 'hold' is supposed to turn off.
e.g. Curve1 is generated using parameterset1. Pressing the clear1 button should turn hold off for curve1 so that new values can be put in parameterset1 and a new curve1 can be plotted on same axes as curve2, curve3 and curve4, without affecting these other curves.
There is a problem with the execution of the 'hold on' and 'hold off' lines in that I cannot delete one curve at a time and replace it with a new one.
The moment I enter new parameters into any of the handles, all the other plots disappear.
Where could I be going wrong?
Is it possible to replace one curve at a time in a single axis with hold on in MATLAB?
Thanks a lot.
Well that is how hold is supposed to work. If you would like to overwrite individual items, just reassign the data that each plot object holds. For example, if you want to replace the handles.temp1 object's data, it would look like:
set(handles.temp1, 'XData', (New xdata), 'YData', (new ydata))
%You might need a drawnow() here but not sure.
You can also update other line object properties the exact same way using set().
Also, note that hold never is off, because otherwise the next plot item will overwrite everything else.
Edit: I would also like to point out that setting handles.temp1 = 0 is not what you are trying to do. Note that a line object has "properties", which is really what you are trying to manipulate. See this for line properties. If you don't know anything about objects and OOP see this
Related
I have written a function of the form
function myplot(x,y)
plot(x,y)
end
This function creates a plot for given values of x and y. The actual function is more complex, but it does not serve the purpose of the question to include its content here. The main point follows.
I have tried to run the following script:
x = [1:0.01:10]
y = [1:0.01:10]
figure
plot(sin([1:0.01:10]))
hold on
myplot(x,y)
The intent here is to plot 2 sets of data on the same graph. The first set of data is generated by Matlab's native plot command while the second set of data is generated by the user custom myplot function (in this case what should be a straight line). The script above wont do it....
How to get Matlab to include both sets of data on the same plot?
your script plots them both but with different x values. if you don't specify x input in plot it uses 1:length(y), while your myplot function does specify x values (which in your case are 10-fold smaller).
just do: plot(x,sin([1:0.01:10])) instead of plot(sin([1:0.01:10]))
You could save the current axes (on which you create the first plot) in a variable and pass it as an argument to your function to be sure it gets plot on the same axes no matter what happens elsewhere in your code.
So your main code could look like this:
x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)
and the function:
function myplot(x,y,hAxes)
plot(hAxes,x,y);
end
Inside a while loop, I have some function that creates all the neccesary y-values for the plot I want to make. After all the y-values are done I want my program to plot the dat(while still inside the loop), but the plot can't be made because the data won't come out until the end of the loop.
Is there anyway to do this?
Basically my code is(and I'm just going to for the first case here)
while c~=3
c=menu('a','b','c')
switch c
case 1
for
%function that creates y-values
end
plot(x,y)
end
end
As I said; I get out all the data at the end of the loop, which is stored in the workspace. Meaning that when I run it a second time, it works fine.
But I want to know how to make it work the first time.
for Continuous line plot you can use drawnow and here it is explained how to do this (remember to use pause(.) if you want to visualize the changes "real-time".
for retain current plot when adding new plots use hold on as it is explained here
if you want to open different windows for every different plot you can use something like:
ii=1;
while ...
...
figure(ii)
plot(x,y)
ii=ii+1;
...
end
but be careful with the last one: if you have a big number of plots you can have some problem
I have trying to plot solutions to coupled ODEs.I solve them using ODE15s solver and pass arguments using a for loop.
MATLAB code then returns a set of plots to me(using hold on).Now only one of the given graphs is appropriate to my theory.So,how do I identify the argument(that I had passed through the for loop) which corresponds to my chosen graph(I can pick out the graph visually)
Just use 'text' function to mark the arguments nearby the each graph.There is a very simple demo(a is the argument):
x=0:100;
figure;
hold on;
for a=1:10
plot(x,a*x);
text(100,a*100,sprintf('a=%s',num2str(a)));
end
Sorry for not pasting picture here because my reputation is not enough.
Many of the plotting functions in MATLAB and toolboxes (thought not all) allow both the following syntaxes:
plotfcn(data1, data2, ...)
plotfcn(axes_handle, data1, data2, ...)
The first plots into the current axes (gca) or creates and plots into a new axes if none exists. The second plots into the axes with handle axes_handle.
Having looked into the internals of several MATLAB and toolbox plotting functions, it looks like there isn't really a standardised way that MathWorks do this. Some plotting routines use the internal, but open, function axescheck to parse the input arguments; some do a simple check on the first input argument; and some use a more complex input-parsing subfunction that can handle a larger variety of input syntaxes.
Note that axescheck appears to use an undocumented syntax of ishghandle - the doc says that ishghandle takes only one input, returning true if it is any Handle Graphics object; but axescheck calls it as ishghandle(h, 'axes'), which returns true only if it's specifically an axes object.
Is anyone aware of a best practice or standard for implementing this syntax? If not, which way have you found to be most robust?
In case anyone is still interested, four years after I posted the question, this is the pattern that I have mostly settled on.
function varargout = myplotfcn(varargin)
% MYPLOTFCN Example plotting function.
%
% MYPLOTFCN(...) creates an example plot.
%
% MYPLOTFCN(AXES_HANDLE, ...) plots into the axes object with handle
% AXES_HANDLE instead of the current axes object (gca).
%
% H = MYPLOTFCN(...) returns the handle of the axes of the plot.
% Check the number of output arguments.
nargoutchk(0,1);
% Parse possible axes input.
[cax, args, ~] = axescheck(varargin{:});
% Get handle to either the requested or a new axis.
if isempty(cax)
hax = gca;
else
hax = cax;
end
% At this point, |hax| refers either to a supplied axes handle,
% or to |gca| if none was supplied; and |args| is a cell array of the
% remaining inputs, just like a normal |varargin| input.
% Set hold to on, retaining the previous hold state to reinstate later.
prevHoldState = ishold(hax);
hold(hax, 'on')
% Do the actual plotting here, plotting into |hax| using |args|.
% Set the hold state of the axis to its previous state.
switch prevHoldState
case 0
hold(hax,'off')
case 1
hold(hax,'on')
end
% Output a handle to the axes if requested.
if nargout == 1
varargout{1} = hax;
end
not sure I understand the question.
What I do is to separate the plotting of data from the generation / setup of plots. So if I want to plot a histogram in a standardized way I have a function called setup_histogram(some, params) which will return the appropriate handles. Then I have a function update_histogram(with, some, data, and, params) which will write the data into the appropriate handles.
This works very well, if you have to plot lots of data the same way.
Two recommendations from the sideline:
Don't go undocumented if you don't need to.
If a simple check is sufficient, this would have my personal preference.
I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.
My approach was as follows:
for i=1:N,
...
//compute m
...
plot(m);
end;
but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?
I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.
As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:
hLine = plot(nan); % Initialize a plot line (which isn't displayed yet
% because the values are NaN)
for i = 1:N % Loop N times
...
% Compute m here
...
set(hLine, 'YData', m); % Update the y data of the line
drawnow % Force the graphics to update immediately
end
In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.
You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.