How can I refresh input data and update plot in MATLAB GUI? - matlab

I am working with Matlab GUI. My problem is the plots are not updated when I change the input data. My code is long but here is the plotting function I am using:
axes(handles.Diagram1)
hold all
for i=1:6:numel(t)
plot(rn,E(i,:)/1000000)
end
set(axesHandle,'Diagram1','Diagram1');
The tag of the axis plot is "Diagram1!
How can I fix this?

MATLAB plots are not permanently linked to the data they display, so if you change the data after plotting, the plot will not be automatically updated. You would need to update the plot yourself after changing the data by reexecuting the plot command.

I've never used it myself, but you're might looking for the drawnow function - see documentation here

You can create a "clean figure" button, that 1) clears current axis (cla), 2) removes the legend, 3) clears the title, and sets any counter to 1. The figure is still there, but its contents are gone. Or you just include the code inside an "if":
function cleanbutton_Callback(source,eventdata)
cla
legend off
title ''
counter = 1;
end
Is this what you need?

Related

MatLab GUI plotting a line that updates with button press

So I'm working on MatLab GUI assignment right now.
It's basically an estimation game. In every trial the user guesses the correlation displayed on the left axes. When they click submit my code calculates the absolute value of the difference between their estimation and the actual correlation.
So far so good.
On the right axes I want to plot a line that updates every time they click "submit". The x-coordinate would be the trial # and the y-coordinate would be the absolute difference previously mentioned.
I can plot this information successfully using points instead of a line by using "scatter" or "plot", but when I try to make it a line, nothing appears, although the axis does seem to update...
Both of the following codes work if the marker is '.' or 'o' or 's' or 'x' ... literally any marker.. but I can't get it to connect the dots... I've messed around with trying to use animated line and drawnow but that didn't work out for me either..
plot(handles.trial, handles.diff(handles.trial),'-.'); hold on; %plot trialwise absolute differences
or
scatter(handles.trial, handles.diff(handles.trial),'-.'); hold on; %plot trialwise absolute differences
**Problem solved!
see the solution below
Solution:
Store all of the relevant data in a matrix handles.DATA. For this to work there needed to be something in there before the first trial so during initialization I set handles.DATA = [0 0] and then with the button-press (submit) that ends a trial, the data from that trial got concatenated into the data matrix: handles.DATA = vertcat(handles.DATA, [x y]). Below this I could do the plot that I wanted to: plot(handles.DATA(:,1), handles.DATA(:,2),'Color','r'
*note: don't hold on because then you will have lines stacking on each other.
**also if you don't specify line color, the line will become a different color every time you click "submit" because this is generating a new line every time based off the updated information.

Redraw a figure saved in 2013b in 2014b

As MATLAB has changed its figure engine in R2014b I decided to rerun some of my code for getting better looking figures out of them. Unfortunately, the last one I have is a code that takes ages to run, and I would like to highly avoid to rerun the code for a nicer figure.
I saved the result in a .fig file in R2013b. However, if I open it in R2014b, it still has the old format.
Is it possible to redraw the figure using the MATLAB R2014b plotting engine? If it is, how could I do it?
NOTE: Literally, the figure is opened and drawn with the new engine, however, it retains its old format. While a new figure with a title() command would plot a nice big, bold title, if a redraw this figure using "drawnow" or I generate code for it, the format remains the same.
Example: This figure was created in 2013b, and redrawn in 2014b. You can see that the title does not plot in the same format as a title('whatever') would plot in the new graphic handles. It looks like that a '.fig' saves and sets the default values for the version it has been generated. Thus plot colors, titles, labels etc will look like the old graphic handles when redrawn.
This can be tested with the following code. Note that this is an overly simplified problem, the question is not explicitly about titles or labels, but all graphic stuff in general.
rng(1)
figure()
x = 1:50;
y = rand(1, 50);
plot(x,y)
title('this NICE Title')
xlabel('labels!')
ylabel('some other labels','Interpreter','Latex')
If this code is run in 2013b and 2014b, saved as fig in both and then opened as fig in both, the next 2 figures appear:
The 2013b fig file: http://s000.tinyupload.com/index.php?file_id=02053933004513599550
There is a roundabout way for doing this -- just using hgopen for loading the figure and then extracting the data to re-plot it in 2014b:
h1=hgopen('test.fig'); % h1 = handle to the figure
allaxes=get(h1,'children'); % allaxes = array with axes handles
for a=1:length(allaxes)
ax=allaxes(a);
allines=get(ax,'children'); % all lines in current axes
for l=1:length(allines)
lin=allines(l);
values=get(lin,'ydata'); % values of the current line
subplots{a}{l}=values;
end
end
You can then use the subplots cell array to make the plots again by hand. It is a boring way to do it, but may be worth trying if re-generating the output takes very long.

How to force drawnow Matlab GUI to draw in new window?

I have used drawnow to draw the characters of the mnist dataset.. which outputs the following output
when i created GUI with matlab and calling drawnow to display images after loading it draws the figure on the open window giving the following output
my question is how to force it to draw in new window ?
drawnow only asks Matlab to flush the event queue and update figure windows; it doesn't determine how and where things are plotted. It's hard to tell since you don't include any code, but in your case it looks like you just plot the character images and the GUI elements into the same figure.
You can control which figure window a graphics operation refers to by setting the "current figure", whose handle is always contained in the variable gcf (graphics: current figure).
You generate a new figure and make it current by calling
figure
If you want to later make this figure current again, you need to save its handle:
fa = figure;
You then make a figure with a given handle current again by
figure(fa)
Some rough sketch of a possible program:
% generate figure windows
fa = figure;
fb = figure;
% plot something in figure a and make the screen update
figure(fa)
plot(...)
drawnow
% put a UI element into figure b and make the screen update
figure(fb)
uicontrol(...)
drawnow

Clearing isosurface (or other plots) from an axes

I use the following command:
isosurface(data,color)
Now if I use the same command with different data again, it gets superimposed on the previous one.
So, I tried doing:
p = patch(isosurface(foo));
isonormals(foo,p)
delete(p);
to delete the previous plot, but this way i can't use the colorbar.
How do I go about this?
Have you tried this?
cla(gca)
cla clears an axes and gca is the handle to the current axes you're are plotting on.
Assuming you just want another patch plot in the same figure window, just use
hold off

How to create a new figure in MATLAB?

Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);