Command for cutting matlab plot from the middle - matlab

I am trying to plot a dataset and want to see a small portion of the plot in the display. The problem is that the dataset is huge and when I am running the algorithm on the whole dataset the significant change in the plot is not clear (visually). Thats why I want to run the algorithm on the whole dataset and want to view a portion of the resulting plot in the display, say 10% of the plot from the middle. Can anyone please help me how to cut the M-figure from the middle - 10% of the original plot? Thanks in advance.

Suppose you're plotting x.
L = length(x);
fraction = 0.1; %#plot 10% of signal
n = round(L*fraction); %#number of samples/points to plot
offset = 0.5 %#start plotting at 50% of the signal (middle)
s = round(L*offset) %#number of samples/points to skip before plotting
t = s:s+n-1; %#discrete 'time' vector, so you know where you are on the plot
plot(t,x(t)); %#plot the selected signal portion

Do xlim, ylim and zlim do what you want?

Related

Multiple vertical histograms plot in matlab

Is it possible to make multiple vertical histograms plot in Matlab into one? Much like the excel sheet enclosed ( https://drive.google.com/file/d/1H_mbyrIoln3XrnK1hLajnVNBKn13y_np/view?usp=sharing )
I want to make a plot of many vertical histogram plots into one figure, by importing excel-files, where on the y axis it has the elevation, the x axis the distance between the histogram vertical lines and the length of the histogram bars is the values in the excel sheet. The vertical height of each bar is 5.
Is this even possible? I have to put in a number of conditions for Matlab to know where to plot, but could some one show me the basic methodology?
Help would be very much appreciated!
The problem is that the parent of a Baseline object is the Axis, which prevents us from doing something like
barh(bins1,counts1,'Basevalue',baseline1); hold on;
barh(bins2,counts2,'Basevalue',baseline2); hold off;
because the plots will automatically share the second baseline value set. There might be a workaround for this that I do not know of, so I invite anybody who knows it to show me how its done.
For now, I was able to sort-of replicate the plot you posted a picture of in a much less elegant way. I will post code below, but before I do, I would like to argue against the use of a plot like this. Why? Because I think it is confusing, as the x-axis both relates to the plot number as well as the bin count numbers. You are in fact trying to display a 3-D data set, the three dimensions being bins, bin counts, and 'histogram number'. A plethora of methods exist for displaying 3-D data, and a series of 2-D histograms may not be the best way to go.
That being said, here is a code that more-or-less creates the picture above, as promised. Any changes you may want to make will be more cumbersome than usual :-)
testData = randn(10000,1); % Generate some data
[counts,bins] = hist(testData); % Bin the data
% First histogram
baseline1 = 0;
p1=subplot(1,3,1); barh(bins,counts,'BaseValue',baseline1);
xticks(baseline1); xticklabels({0}); % Graph number on x axis at baseline (0)
box off; % Remove box on right side of plot
ylabel('Property');
% Second histogram
baseline2 = max(counts)*1.2;
sepdist = baseline2-baseline1; % Distance that separates two baselines
counts2 = baseline2 + counts;
p2=subplot(1,3,2); barh(bins,counts2,'BaseValue',baseline2)
xticks(baseline2); xticklabels({1}); % Graph number on x axis at baseline
box off;
Y=gca; Y.YAxis.Visible='off';
p1p=p1.Position; p2p=p2.Position;
p2p(1)=p1p(1)+p1p(3); p2.Position=p2p; % Move subplot so they touch
% Third histogram
baseline3 = baseline2 + sepdist;
counts3 = baseline3+counts;
p3=subplot(1,3,3); barh(bins,counts3,'BaseValue',baseline3)
xticks(baseline3); xticklabels({2});
Y=gca; Y.YAxis.Visible='off';
box off
p3p=p3.Position;
p3p(1)=p2p(1)+p2p(3); p3.Position=p3p;
% Add x-label when you are done:
xl=xlabel('Test xlabel'); xl.Units='normalized';
% Fiddle around with xl.Position(1) until you find a good centering:
xl.Position(1) = -0.49;
Result:

MATLAB: Two different y-axis limits for Multiple plots on same graph

I need to plot two plots on same figure in MATLAB.
The maximum and minimum values in both the data samples have large variation, which I am unable to plot by taking same y-axis limits.
I do not wish to use two scales as explained in other Overlaying two axes in a Matlab plot but need to use a single y-axis and get the solution.
I tried the code:
x_axis_X = 1:length(S);
y_axis_Y = 1:length(N);
ylim([-1204200 -1841.6])
set(gcf,'color','w');
plot(x_axis_X, S,'o-', y_axis_Y, N, 'x-');
The result is as shown in the plot where one data sample is plotted without proper y-axis range.
The y limits for first data sample is -1204200 to -1841.6 and for the second it is -489429345.5 to -10408189.43.
How should be the ylim defined to fit both plots in the same figure?
I appreciate your inputs. Thank you.
In older versions of MATLAB use the function plotyy. In more recent versions of MATLAB use yyaxis. The following is the example from the documentation:
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
I tried the idea of scaling one dataset so that it has a similar magnitude as the other data set. Here, I multiplied one dataset by 100 (or any suitable scaling parameter), and then it will be similar in size to the other data set. In order to clearly mention which data has been scaled in the graph I used the legend.
plot(x,data1,x,100*data2)
legend('data1','100*data2','location','southeast')
Thank you.
Scaling is not the best option, as you may need to work with the data later. Also does not work if for instance, you need a log scale.
Matlab has a few ways to deal it with. I particularly like to use a new axes in the figure, as i have done in the example below.
Just in case, you also found this answer in a simple google search!
Code:
a=1:10;
b=(10:-1:1)*100;
x=1:10;
hold on
plot(x,a,'b')
pax1=get(gca,'Position'); %get axis position
ax2 = axes('Position',pax1); %create a new axis
plot(ax2,x,b,'r') %plot new data
set(ax2, 'Yaxislocation','right',...
'color','none') % set it transparent and to the right

matlab contour plot time depth density

I need assistance making a nice contour plot. I have data from an underwater glider that dives and climbs repeatedly from the surface of the ocean to around 30 m, in this case.
I think my issue is with interpolating the data, and I am not sure how to proceed. Here is the contour plot of density I have generated this far.
The Contour plot of density was generated using this code
xlin = linspace(min(time),max(time),500);
ylin = linspace(min(depth),max(depth),500);
[X,Y] = meshgrid(xlin,ylin);
Z = griddata(time,depth,density,X,Y);
[C,h] = contour(X,Y,Z,[1022.0, 1022.5, 1023.0, 1023.5, 1024.0, 1024.5, 1025.0, 1025.5, 1026.0],'color',[0.5 0.5 0.5]);
v = [1022.0, 1022.5, 1023.0, 1023.5, 1024.0, 1024.5, 1025.0, 1025.5, 1026.0];
clabel(C,h,v,'fontsize',8);
set(gca,'ydir','reverse');
I want the plot to have smooth contour lines. Once I get the contour plot to look good I will overlay it on salinity and temperature scatter plots.
Please let me know how I can make a better looking contour plot.
Is it an issue with interpolation? Or the way I gridded the data?
Thanks very much! Please be specific and give code examples if you've played with the data.
Here is the time, depth, and density matlab data: https://www.dropbox.com/s/agi70zh7haggf07/data.mat?dl=0
The problem is that bunch of your interpolated data are missing. I mean that Z has a bunch of NaNs:
xlin = linspace(min(time),max(time),500);
ylin = linspace(min(depth),max(depth),500);
[X,Y] = meshgrid(xlin,ylin);
Z = griddata(time,depth,density,X,Y);
%surf(X,Y,Z) %also interesting
spy(isnan(Z));
Result:
Your input data are somehow ill-defined, and griddata gives up. Here's why:
>> sum(isnan(density))
ans =
3174
Fix the NaNs in your raw data, and you'll most probably fix the plot.
Update
I threw away your NaNs:
inds=~isnan(density);
time=time(inds);
depth=depth(inds);
density=density(inds);
to see how the result looks like. It turns out that your original code is already looking OK to me!
Original on the left, de-NaNed version on the right:
So... maybe your datetime transformation is off? Or your time limits, not showed in your original code?

MATLAB - Plot half the Nyquist Plot

Is there a way to only plot half the nyquist plot in MATLAB? Nyquist usually has the two lines that go in opposite directions (one going up, and one going down). I only want to plot the one going down.
I know that I can only show the negative axis to "hide" the positive bit of the plot, but I want to show the whole chart, but only have the one line:
Okay, I got the answer. Use nyquistplot instead of nyquist and set the option ShowFullContour to off:
% This is an example contour
G = tf(80, [1 9 30 40]);
h = nyquistplot(G);
setoptions(h, 'ShowFullContour', 'off');

Is there any way to restrict the plots in one grapgh?

I'm trying to plot several signals in one graph and i would like to restrict them and sort of minimize it so all my signals will be clear (more or less).
I have no idea how to do it. I'm adding an image so what i need to do will be clearer.
My data changes but in general it's the mean intensity of each column of a certain area in an intensity image.I tried to do it like with the same idea as you but i don't get the right plot as i wanted. A is the relevant matrix,b is the matrix with shifted values:
for i=1:20
b(i,:)=A(i,:)+(100*i);
plot(b(i,:))
hold on
end
I will also add 2 images: one is the plot of all the 20 signals that i get and the other one is the plot of only the first signal. I don't understand why do they look so different.
You can try something like that :
x = [1:100]; %Distance 1 to 100
y = F(x) % Your first function (signal)
y2 = 0.5*G(x) % Your second function (signal)
plot(x,y,x,y2); % plot both function in a single plot.
hleg1 = legend('Intensity t1,'Intensity t27');
So you have your signal at intensity t27 half cut for each value ( 0.5 ), so it shift down.