MatLab GUI plotting a line that updates with button press - matlab

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.

Related

Unable to plot multiple data sets in GUIDE

I'm using GUIDE to display a plot within an axis that contains two data sets: the original signal and the average of the signal, but for some reason it seems to only plot one.
The axis is designated as m_graph and the data sets are avg and signal, which both share time.
plot(handles.m_graph, time,signal)
hold on
plot(handles.m_graph, time, avg)
When I compile the program, only the average is plotted. It seems to skip over the original signal or reset the axis. I've tried plotting just the signal so I know the data is fine.
I feel like I am missing something, maybe the set function?
Sorry my reasoning was a bit wrong; it applies to the current selected axes (it does not parent to the Figure).
However, using axes(h) followed by hold on or just hold(h,'on') will either switch the focus to the axes then turn hold on or turn hold on for a specified axes, respectively.

Axis commands changes TightInset property to zero

I use some things from this question get-rid-of-the-white-space-around-matlab-figures-pdf-output to get rid of the white space when saving figure plots and images. This works fine, but my problem is when I use commands like "axis square" or "axis image". Ivt sets TightInset property of corresponding axes to 0 in "y" direction. I mean when I use this:
inset=get(a,'TightInset');
second and fourth numbers in "inset" are always zero, even if I set title and x-label. So in the end I don't see plot titles and x-labels.
Can I fix it somehow? So far I do it manually by adding some suitable number, but it is not convenient.
EDIT: Some more code for example. It displays two histograms.
h=figure;
subplot(121)
bar(bins,histogram1,'hist');
axis square
title('Historgram 1');
xlabel('Intensity');
ylabel('Pixel count');
set(gca,'xlim',[0 1])
subplot(122)
bar(bins,histogram_out,'hist');
axis square
title('Histogram 2');
set(gca,'xlim',[0 1])
xlabel('Intensity');
ylabel('Pixel count');
and if I call
a=get(h,'Children');
for i=1:length(a)
inset=get(a(i),'TightInset');
%...some stuff here
end
those y-related numbers in inset are zeros. If I comment axis square and do this, then inset are correct and it does what I need.
As far as I know when you use 'TightInset' you'll get only the graph or image, axis will be removed. I downloaded a function from mathworks file exchange 'saveTightfigure.m' to solve a similar problem. But I did not needed axes. If you need axes may be you can edit limits set inside the function. I mean you can give a little more space at left and bottom for keeping the axes.

matlab: plot area continues after data ends

I have a simple plot in Matlab but as you can see from the screenshot. There is a lot of white space on the right side of the graph after the end of the data series.
Any idea how to get rid of this white space and make the plot go right to the edge of the figure? Here is my code:
Plotx = plot(x);
hold on
PlotState = plot(Y);
set(Plotx,'Color','black','LineWidth',2.5);
set(PlotState,'Color','red','LineWidth',2.5);
set(gca, 'XTick',(1:3:62))
labels = time;
set(gca,'XTickLabel',labels(1:3:62))
grid on
This usually works for me:
axis tight;
xlim('auto');
You must select your figure, go back to the console and use those commands so they affect the last active figure.
EDIT: The above line should automatically make your graph axis very tight on your data. For more fine control, you may want to define the axis limits manually:
axis([xmin,xmax,ymin,ymax])
I found a solution. I manually adjust the limit of the x-axis, using:
set(gca,'XLim',[0 63])

MATLAB: Plotting two different axes on one figure

The MATLAB surf plot below is essentially two plots plotted adjacent to each other. For clarity, I have included some code below used to prepare the plot:
band1 = horzcat(band1, eSurface2(:,:,1));
band2 = horzcat(band2, eSurface2(:,:,2));
surf(band2,'DisplayName','band2');
surf(band3,'DisplayName','band2');
I would like for the y axis numbering to restart at the start of the second graph. How do I go about doing that?
You can use the 'YTick' and 'YTickLabel' properties of the axis to control the ticks, this way you can make it start from zero for the second graph. It will require some trail and error to get it right. See the relevant doc here (you'll have to scroll all the way to the bottom of the page).
Take advantage of the following feature of 'YTickLabel': "If you do not specify enough text labels for all the tick marks, MATLAB uses all of the labels specified, then reuses the specified labels".

Why won't this axes object display correctly in MATLAB?

I am writing two small psychoacoustic testing applications in MATLAB. The first one works without problems but the second one doesn't, and I just can't figure out why.
Here is the problem: the axes object is created, but it is empty.
failed_axis http://dl.getdropbox.com/u/98854/help.png
Here is the code that creates this figure and axes:
hFig = figure('dockcontrols','off','menubar','none', ...
'name','choose the better sounding file', ...
'numbertitle','off','position',[0,0,700,500], ...
'resize','off','toolbar','none','units','normalized', ...
'color',[.8,.8,.8]);
progress_idc = axes('position',[.1,.8,.8,.05],'box','on','parent',hFig,...
'xlim',[-.03,1.03],'xtickmode','manual','xtick',[], ...
'xticklabelmode','manual','xticklabel',[], ...
'ylim',[-1,1],'ytickmode','manual','ytick',[], ...
'yticklabelmode','manual','yticklabel',[], ...
'nextplot','add');
And here is the code that plots in this axes (the function is called regularly by a timer):
function replot(varargin) % this is a nested function
cla;
% plot start_indicator
plot([x_start,x_start],[-.7,.7],'k','linewidth',2);
fill([x_start,x_start-.02,x_start-.02],[0,-.7,.7],[0,0,0]);
% plot stop_indicator
plot([x_stop,x_stop],[-.7,.7],'k','linewidth',2);
fill([x_stop,x_stop+.02,x_stop+.02],[0,-.7,.7],[0,0,0]);
% plot play_position
plot([x_play,x_play],[-1,1],'r');
drawnow;
end
This is what it looks like if it works:
proper_axis http://dl.getdropbox.com/u/98854/help2.png
Do you have any idea what is going wrong here?
I ran the code you included above and got the correct output.
If I had to take a wild guess as to what the problem is, I'd guess that you may be creating other axes in your application that you are not listing above, or that you may have other axes not related to the application open at the time the application is running. When you plot your objects in the function replot, you are by default plotting them to the currently active axes. If you have multiple axes open, the plotting may therefore be going on in the wrong set of axes.
One suggestion I would make is to explicitly specify what the parent axes object should be in your calls to PLOT and FILL. If you add the arguments ...,'Parent',progress_idc); to your plotting calls, it will ensure that the correct axes is always used. I make a habit of always specifying the parent axes object instead of assuming that the currently active axes will always be the one I need it to be.
I finally found the (dumb) answer. The title accidentally had the same position as the plot axis. Due to some rendering-details of Matlab, it obscures the whole axis except for the rightmost and bottommost line of pixels, which makes the axis look "empty".
Oh, what a dumb error.