
How to find a specific point on a plot and connect it with other points?
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
Hello all,
I am trying to plot a specific point on th plot. For example at first set of data the maximum y value (at0) is 12. I need to divide it by 2 which gives me  the value 6. The nearest corresponding value is in cell A8 i need to plot this value. And simillarly for the other data. I have tried to plot it. But i am not getting the desired results. I have tried and I am attaching the result what i am currently getting. Please help me. Thanks in advance.
My code:
clc
clearvars
close all
Z = readtable('Abb12.xlsx') ;
data = table2array(Z) ; 
N = size(data,2);
Nsp = N/2;
ttlc = {'x=0m', 'x=0.1m', 'x=0.2m', 'x=0.3m', 'x=0.4m', 'x=0.5m', ...
    'x=0.75m', 'x=1.0m', 'x=1.25m', 'x=1.5m', 'x=2.0m', 'x=2.5m', 'x=3.0m', 'x=3.5m'};
xofst = [0 100 200 300 400 500 750 1000 1250 1500 2000 2500 3000 3500];
figure(1)
hold on
for k = 1:Nsp
    col = [2 1]+2*(k-1);
    famp = 5;                                                            
    datacol1 = data(:,col(1))*famp+xofst(k);
    datacol2 = data(:,col(2));
    minx(k) = min(datacol1);
    maxx(k) = max(datacol1);
    miny(k) = min(datacol2);
    maxy(k) = max(datacol2);
    %%%%%%%%%%%%%%%%%%%%% special code for inner line %%%%%%%%%%%%%%%%%%%%%
    x_init = (maxx(k)+minx(k))/2; % initial guess
    % find nearest value
    aa = round(length(datacol1)/2);
    datacol1_pos = datacol1(aa+1:end);
    datacol2_pos = datacol2(aa+1:end);
    [~,ind_pos] = min(abs(datacol1_pos-x_init));
    y_pos(k) = datacol2_pos(ind_pos); % final value
    x_pos(k) = datacol1_pos(ind_pos); % final value
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    hp(k) = plot(datacol1, datacol2, 'LineWidth',2);
    plot([1;1]*[minx(k) maxx(k)], ([1;1]*ylim).', ':k', 'LineWidth',1)             
    Line_Coordinates = [minx(1) maxx(1)]-minx(1)+minx(k);           
    LineLength = diff(Line_Coordinates);     
end
plot(minx,maxy,'dr')
plot(minx,maxy,'r')
plot(x_pos,y_pos,'dr')
plot(x_pos,y_pos,'--r')
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
댓글 수: 0
채택된 답변
  Mathieu NOE
      
 2021년 11월 9일
        hello 
to make it work , you have to remove the NaN content of your data 

clc
clearvars
close all
Z = readtable('Abb12.xlsx') ;
data = table2array(Z) ; 
N = size(data,2);
Nsp = N/2;
ttlc = {'x=0m', 'x=0.1m', 'x=0.2m', 'x=0.3m', 'x=0.4m', 'x=0.5m', ...
    'x=0.75m', 'x=1.0m', 'x=1.25m', 'x=1.5m', 'x=2.0m', 'x=2.5m', 'x=3.0m', 'x=3.5m'};
xofst = [0 100 200 300 400 500 750 1000 1250 1500 2000 2500 3000 3500];
figure(1)
hold on
for k = 1:Nsp
    col = [2 1]+2*(k-1);
    famp = 5;                                                            
    datacol1 = data(:,col(1))*famp+xofst(k);
    datacol2 = data(:,col(2));
    % remove NaN from data 
    ind1 = ~isnan(datacol1);
    ind2 = ~isnan(datacol2);
    ind = ind1 & ind2;
    datacol1 = datacol1(ind);
    datacol2 = datacol2(ind);
    minx(k) = min(datacol1);
    maxx(k) = max(datacol1);
    miny(k) = min(datacol2);
    maxy(k) = max(datacol2);
    %%%%%%%%%%%%%%%%%%%%% special code for inner line %%%%%%%%%%%%%%%%%%%%%
    x_init = (maxx(k)+minx(k))/2; % initial guess
    % find nearest value
    aa = round(length(datacol1)/2);
    datacol1_pos = datacol1(aa+1:end);
    datacol2_pos = datacol2(aa+1:end);
    [~,ind_pos] = min(abs(datacol1_pos-x_init));
    y_pos(k) = datacol2_pos(ind_pos); % final value
    x_pos(k) = datacol1_pos(ind_pos); % final value
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
    hp(k) = plot(datacol1, datacol2, 'LineWidth',2);
    plot([1;1]*[minx(k) maxx(k)], ([1;1]*ylim).', ':k', 'LineWidth',1)             
    Line_Coordinates = [minx(1) maxx(1)]-minx(1)+minx(k);           
    LineLength = diff(Line_Coordinates);     
end
plot(minx,maxy,'dr')
plot(minx,maxy,'r')
plot(x_pos,y_pos,'dr')
plot(x_pos,y_pos,'--r')
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

