The appearance of a dashed line in matlab is not even. For example:
x = [121.2802 112.5784 115.0855 109.0412 99.1158 103.9001];
y = [-25.8392 -24.9378 -25.1976 -24.5714 -23.5433 -24.0389];
plot(x,y,'--k','linewidth',1.2)
print -r600 -dtiff test.tif
The dashes do not appear separate in some parts of the line. Is this because the points are not evenly distributed? Any suggestions? Thanks.
No the problem isn't matlab it is your variables. Matlab is conecting (x(i),y(i)) with (x(i+1),y(i+1)) with a dashed line. But your data isn't sorted.
The only thing happening here is that you draw 2 lines above each other which results in your non dashed parts. If you want only dashed line try sorting your data before plotting it.
Edit 1
z= [x' ,y'];
z= (sortrows(z))';
x2=fliplr(z(1,:));
y2=fliplr(z(2,:));
plot(x2,y2,'--k','linewidth',1.2)
Here is a way to sort your data. What i am doing is i am conecting x,y rowwise in z. Then i use sortrows to sort all rows according to the first one. Then i transpose the resulting matrix so that you have new coloumn vectors. After that i use fliplr to flip the first to last element and so on (inverting your data, since your original data went from 120 to 100and i don't know if you want to use the data later on). And then i plot it.
Hope that helps
Edit 2
As posted by Dennis:
z= [x' ,y'];
z= (sortrows(z))';
z=flipud(z);
plot(x2,y2,'--k','linewidth',1.2)
Related
I'm trying to add legends the plot when it adds a curve. I can't see whats wrong with my code, can someone please help? I'm using matlab r2015a on ubuntu.
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for i=4:10
pl=plot(x,v*i);
[~,~,plots,str]=legend([plots;pl],str,num2str(i))
end
when i run it i get:
plots =
1x2 Line array:
Line Line
str =
'1' '4'
Error using vertcat
Dimensions of matrices being
concatenated are not
consistent.
So its means it works the first lap but not the second.
As discussed in the comment, the way Matlab handles legends is different than in earlier releases; they are now legend objects.
In any case, to solve your problem, which is a dimension mismatch problem, you can simply concatenate vertically the outputs you get using the transpose operator, because Matlab returns a horizontal array of line and text objects whereas you want a vertical array. Therefore, using plots.' and pl.' works fine. Also, it's good practice not to use i as a loop counter since it represents the imaginary unit.
clear
clc
close all
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for k=4:10
pl=plot(x,v*k);
[LegendObject,~,plots,str]=legend([plots.';pl.'],str,num2str(k));
end
%// Use the legend object to modify its properties/location
set(LegendObject,'Location','NorthWest');
Output:
I am getting error for my below code: temp=reshape(img',irow*icol,1);
Error message:Error using '
Transpose on ND array is not defined.
What is solution for this. I think I have to use permute(A,order) command. But I dont know how to use this command in my code. Do you know any solution?
for i=1:M
str=strcat(int2str(i),'.jpg'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
if i==3
title('Training set','fontsize',18)
end
drawnow;
[irow icol]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1); %creates a (N1*N2)x1 matrix
S=[S temp]; %X is a N1*N2xM matrix after finishing the sequence
%this is our S
end
I assume the code was designed for grey scale images. For matrices with more than two dimensions, you have to use permute. One solution could be:
[irow icol d]=size(img);
temp=reshape(permute(img,[2,1,3]),[irow*icol,d]);
Which results in a nx3 matrix, each column corresponding to one colour. You have to change the last line as well, but I don't know what you are expecting. Maybe take a look at cat
I'm currently having some trouble with the 'smooth' command, namely that it seems to have no effect on the generated plot. I have already used the following script to generate a plot
for h=1:4
linespec = {'rx', 'gx', 'bx', 'yx'};
hold on
for b=1:365
y=mean(mean(A(b,6*(h-1)+1:6*h)));
p(h)=plot(b,y,linespec{h});
end
hold off
end
Going row by row in data set A and taking the average of the values in the first six columns, then column 7 through 12, 13 through 18 and 19 through 14; generating four plots in total.
The next step was to smooth the resultant plot by averaging the values over a span of 9. So, I tweaked the script to the following;
for h=1:4
linespec = {'rx', 'gx', 'bx', 'yx'};
hold on
for b=1:365
y=mean(mean(A(b,6*(h-1)+1:6*h)));
w = smooth(y,9,'moving');
p(h)=plot(b,w,linespec{h});
end
hold off
end
Essentially just adding the w variable and replacing y with w in the plot command. Yet this has no effect whatsoever on my plot. Matlab doesn't throw up any errors either, so there doesn't seem to be a problem with the input size. Does anyone have an idea as to what the issue might be?
In either version of the loop, you appear to be plotting individual values of y against individual values of b. I presume, then, that y is a single value. You can't smooth a point, so the smooth operation is having no effect.
From the start, you don't need to make a loop to calculate the various means; mean can take a 2D matrix and return a vector. Calculate y in one go, then smooth that vector (should have length 365, I presume - depends on the size of input A). e.g.:
b = 1:365;
y=mean(A(:,6*(h-1)+1:6*h),2);
w = smooth(y,9,'moving');
plot(b,y,'rx');
hold on
plot(b,w,'gx');
There are 2 column vectors A,B containing 100 data values. I intend to plot the MSE(mean square error ) using the following code but all I get is a single dot instead of a line plot. Please help how to go about it.
A=x(:,1);
B=y(:,1);
er=(double(A)-double(B)).^2;
row_er=mean(er,2); % variable changed
plot(row_er);
This script works fine.
A = randn(10, 1);
B = randn(10, 1);
er=(double(A)-double(B)).^2;
row_e=mean(er,2);
plot(row_e)
Probably you have a typo (row_er)
row_e=mean(er,2);
plot(row_er);
Please notice that the command mean returns the mean of a vector (which is one simple value). If you want to plot plot the square error then you just plot((A-B).^2).
But... If you are interested in plotting the mean square error with, say, 10 samples average, you will have a plot with only 10 points (100 / 10 because each 10 data points are averaged to give you one point).
The command would be
plot(blkproc((A-B).^2,[10,1],'mean'))
hope it helps.
I have this cell (16x1) in MATLAB:
eventIDs =
'explosion'
'light'
'darkness'
'atomic'
...
..
now I want to use this :
%First bar plotting!
bar(duration_vector);
d = size(duration_vector);
labels = cell2mat(eventIDs);
xticklabel_rotate([1:d],45,eventIDs,'interpreter','none');
set(gca, 'XTick', 1:d, 'XTickLabel', labels);
I want to plot a bar graph but my events I too long, and I want them rotated to look good!
but when I run the code
I get this:
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 85
m{n} = cat(1,c{:,n});
Error in ==> extract_data at 52
labels = cell2mat(eventIDs);
You don't need to do cell2mat. That tries to create a 2D matrix of characters (which fails because your strings are different lengths).
You also don't need the set(... line because xticklabel_rotate already sets the labels.
cell2mat in Matlab only works if your cell has a consistent number of columns in all rows. That is so because Matlab can't handle normal arrays with a variable number of columns per row, and that's generally the case of string matrices.
That said, cell manipulations is almost equal to matrix manipulation, the only difference being the indexation method: matrices use square brackets ([) and cells use curly brackets.
I googled the code of this function you're using, the xticklabel_rotate, and found the fileexchange link to the function here. There, the example given by the author uses a cell, instead of a matrix.
So I'm guessing that you can drop this cell2mat off because I thik you don't need to set the Xticks with the set function you're using. The xticklabel_rotate should do that.
I think you should try this:
%First bar plotting!
bar(duration_vector);
d = size(duration_vector);
xticklabel_rotate([1:d],45,eventIDs,'interpreter','none');