Invalid data argument when using plot - matlab

I have a problem with the following MATLAB code:
E=1.15*10^5;
h=0.35;
b=50;
l=75;
i0=(b*h^3)/12;
f=0:1:10;
P=[0 0.1 0.2 0.3 0.3 0.4 0.6 0.7 0.8 1.1 1.4];
Pt=(E*2*10^5*f)/l^3;
plot(f, P,'k','LineWidth', 2, f, Pt, '-.k', 'LineWidth', 2); grid;
xlabel('Sageata f[mm]');
ylabel('Forta P[N]');
legend('P', 'P_t');
title('Caracteristica arcului triunghiular');
Error using plot
Invalid first data argument
First of all, what is k and -k and why my is plot not working without them? Secondly why do I only have one line?

If you want tight control of line parameters, its much better to just separate each line you want to plot in a separate plot call.
hold on
plot(f, P,'k','LineWidth', 2)
plot(f, Pt, '-.k', 'LineWidth', 2);
grid;
hold off

Related

How to make the marker smaller in legend in when plot with matlab

In this figure, the marker in the legend is so big, how can I make it shorter.
I spend a lot of time searching for the solution online. But I did not find any solution that can fix this problem.
Thanks.
This is the code,
latency = [1 1.3 0.5;...
0.8 1.2 0.4;...
0.7 1.1 0.35;...
0.9 1.0 0.3;...
0.8 1.2 0.4;...
0.7 1.1 0.3];
h = bar(latency);
set(h(1), 'FaceColor',[33 36 61]./255)
set(h(2), 'FaceColor',[240 240 240]./255)
set(h(3), 'FaceColor',[250 128 114]./255)
set(h, 'LineWidth', 1);
set(gca,'Linewidth',1,'Fontname', 'arial');
set(gca, 'XLim', [0, size(latency,1)+1])
set(gca, 'YLim', [0 max(max(latency))*1.1]);
xlabel('(a) Latency','FontSize',14)
ylabel('Latency (us)','FontSize',14)
legend({'AAAAAAAA', 'BBBBBBBB', 'CCCCCCCC'}, 'Orientation', 'horizontal','FontSize',11)
box on;
Update:
According to the answer, I add two lines of codes:
[lgd,icons,plots,txt] = legend({'AAAAAAAA', 'BBBBBBBB', 'CCCCCCCC'});
icons(4).Children.XData = icons(4).Children.XData/2;
Now I get this legend.
But there is a blank space before text.
There is an undocumented propertie ItemTokenSize to do that:
% Fixe the new size of each box:
box_size = [10 10 10]
% Get the handle
hdl = legend({'AAA', 'BBB', 'CCC'})
% Set the new size
hdl.ItemTokenSize = box_size;
% Then recenter the legend
% ...
Undocumented means that this option could be deleted, without notice, in a future release.

MATLAB: color scale with probplot

I have two 1D vectors of the same length, data and my_parameter. I use probplot to see the probability plot for data, as follows:
h = probplot(gca, data);
% in next line, want color based on my_parameter, not static color as used.
set(h, 'color', [0.5 0.5 0.5]);
set(h, 'marker', '.');
I would like to color by my_parameter, the goal being to see whether certain values of my_parameter throw off the normality of the distribution. Is there a way to use a color scale, e.g., parula, in conjunction with the probplot function?
I have tried:
Replacing the [0.5 0.5 0.5] with 'parula'.
Replacing the [0.5 0.5 0.5] with parula.
Replacing the [0.5 0.5 0.5] with a m-row 3-column double matrix, where each row has the rgb values that parula would map my_parameter to. (So m is the length of my_parameter.)
Getting rid of the line set(h, 'color', [0.5 0.5 0.5]);, and adding a line colormap(parula); below the set lines.
If there is not a way to do this using the probplot function directly because of how it is written (e.g. if it is written to accept only 3-element vectors), I guess I'll have to try to rewrite my own version of probplot using one of the scatter functions. I could dig around and figure this out, but before I start doing this, could anyone point me to a resource where this is done already perchance?
Thanks for any help.
Try this:
x=rand(1,15); % dummy data to plot
probplot(x)
h = probplot(gca,x);
c = parula(10);
% this changes the color of the 'x' marks to be the first row of the parula matrix
h(1).Color = c(1,:);
% this changes the color of the dashed line to be the 9th row of the parula matrix
h(2).Color = c(9,:)
The above produces this:

How to plot point and vectors in Matlab

A = [239920.240412166 1.31193313030682;
243577.444235102 1.38185205485119;
241899.250050298 1.51264147493485;
244659.326936560 1.50845243215867;
239862.361809342 1.50810833389632;
238395.616682194 1.37125000688350;
244558.389789124 1.27212093329482;
244290.890880318 1.35116080948488;
240303.711239396 1.36064181572699;
237464.430450140 1.48857869573721;
244415.381196104 1.51252425335623;
239855.328594799 1.29178640586301;
239304.806448742 1.31075813783171;
244827.243024016 1.32080934043223;
241465.885648910 1.53667019314427;
241139.254464482 1.40424079027764;
242300.037630214 1.27160249886092;
243330.396959248 1.61411410292679;
237530.389940994 1.21846939260826];
B = [0.6 0.18; 0.15 0.46]; % green circles
for i=1:2
plot(A(:,1),A(:,2),'r*');
hold on
plot(B(i,1),B(i,2), '-ko',...
'LineWidth',1,...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',9);
end
When I ploted A and B, I got this:
B(1,1) = 0.6, but it appears in 0 (X-axis). Same with B(2,1) = 0.15
How to correct this?
A logarithmic scale on the xaxis will help with the view
set(gca, 'XScale', 'log')
However, it will lead to the fact, that now the values of A appear to populate one vertical line.
If you cannot live with this, you may want to try a broken x-axis. MATLAB doesn't support this with build-in functions, but there is a solution in the MATLAB file exchange
https://de.mathworks.com/matlabcentral/fileexchange/3683-breakxaxis
Btw: There is no need for the loop in your code. In fact you plot A twice on top of each other. Just
% Plot A and B without loop
plot(A(:,1), A(:,2),'r*')
hold on
plot(B(:,1), B(:,2), '-ko', 'LineWidth', 1, ...
'MarkerFaceColor', [.49 1 .63], 'MarkerSize',9)
% Set x axis to logarithmic scale
set(gca, 'XScale', 'log')
is sufficient to display your plot
Your x-axis goes from 0 to 250000. On that range, 0.6 and 0.15 are practically 0.
If you want you can use logarithmic scale in x-axis using semilogx

Anti-alias lines vs markers in MATLAB

Hi I have a image in MATLAB
and I want the line to be smooth - look at the line from 0.4 to 0.8... it's horrible.
When using 'LineSmoothing','on' operator in plot I get this
I does a good job on lines but it smooths markers also and they are horrible!!
How can I get MATLAB to smooth only lines and not the markers??
Here is the code:
clear all;
close all;
bpp = [0.8 0.4 0.2 0.1 0.05];
bpp_j = [0.8 0.4 0.2 0.1];
AAE_JPEG = [1.65 2.91 6.20 10.96];
AAE_JPEG_2000 = [1.39 2.29 3.78 6.75 12.52];
AAE_EEDC = [2.08 2.67 3.80 5.94 9.31];
hold on;
plot(bpp_j, AAE_JPEG, 'k','LineWidth',1.5,'MarkerSize',9,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp, AAE_JPEG_2000, 'k', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp, AAE_EEDC, 'k', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k','LineSmoothing','on');
plot(bpp_j, AAE_JPEG, 'x','LineWidth',1.5,'MarkerSize',8,'MarkerEdgeColor','k');
plot(bpp, AAE_JPEG_2000, 'o', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k');
plot(bpp, AAE_EEDC, 'v', 'LineWidth',1.5,'MarkerSize',6,'MarkerEdgeColor','k');
LL = plot(rand(1,2),rand(1,2),'k-x','visible','off','LineWidth',1.5,'MarkerSize',8);
LK = plot(rand(1,2),rand(1,2),'k-o','visible','off','LineWidth',1.5,'MarkerSize',6);
LI = plot(rand(1,2),rand(1,2),'k-v','visible','off','LineWidth',1.5,'MarkerSize',6);
legend([LL,LK, LI],'JPEG','JPEG 2000','EEDC')
axis([0 0.9 0 14])
xlabel('bpp');
ylabel('AAE');
grid on;
and while I'm still here... how can I only display 0.05 0.1 0.2 0.4 and 0.8 on x-axis?
I'd just try using export_fig without even linesmoothing the lines...
I don't have a MATLAB here so I can't test but does it work if you plot the smoothed lines without markers
plot(bpp_j, AAE_JPEG, 'k','LineWidth',1.5,'LineSmoothing','on');
then another plot of the markers with no lines?
plot(bpp_j, AAE_JPEG, 'x','MarkerSize',8,'MarkerEdgeColor','k');
As for the x-axis ticks see matlab x axis label set as a vector

How to keep the subplot sizes unchanged after putting a colorbar

Let us say we have a 1-by-2 subplot and we plot some graphics inside as follows:
subplot(1,2,1)
surf(peaks(20))
subplot(1,2,2)
surf(peaks(20))
And then we want to put a colorbar:
colorbar
I don't want the right figure squezzed as in the result. How can we put the colorbar out of the rightmost figure in a row of subplots and keep the sizes of them unchanged?
Note: Actually, I need it for plotting images where the colorbar is common and I want to put it on the right. I used this toy example for simplicity.
You could just extract the position of the first plot and use on the second. MATLAB automatically moves the colorbar to the right when rescaling.
f1=figure(1);clf;
s1=subplot(1,2,1);
surf(peaks(20));
s2=subplot(1,2,2);
surf(peaks(20));
hb = colorbar('location','eastoutside');
%% # Solution:
s1Pos = get(s1,'position');
s2Pos = get(s2,'position');
s2Pos(3:4) = [s1Pos(3:4)];
set(s2,'position',s2Pos);
%% # Alternative method. Brute force placement
set(s1,'Units','normalized', 'position', [0.1 0.2 0.3 0.6]);
set(s2,'Units','normalized', 'position', [0.5 0.2 0.3 0.6]);
set(hb,'Units','normalized', 'position', [0.9 0.2 0.05 0.6]);
This is just what I was looking for. After implementing Vidar's automatic solution I came up with a simplification. Get the position of the far right axes BEFORE adding the colorbar, and then just reset the squeezed position to the original:
f1=figure(1);clf;
s1=subplot(1,2,1);
surf(peaks(20));
s2=subplot(1,2,2);
surf(peaks(20));
s2Pos = get(s2,'position');
hb = colorbar('location','eastoutside');
set(s2,'position',s2Pos);