Как сдвинуть построенную линию в фигуре Matlab - PullRequest
0 голосов
/ 28 февраля 2019

Я строю график греха и косинуса, используя следующий код:

x = 0 : 0.1 : 2*pi ;
y1 = sin (x) ;
y2 = cos (x) ;
figure ;
plot(x,y1) ;
hold ;
plot(x,y2,'r')

Я хочу переместить / сдвинуть линию, соответствующую греху, чтобы наложить на верхнюю часть строки, соответствующей cos, и хочу достичь этогоне двигая кастрюлю.Я искал сеть, но не смог найти простое решение.

Ответы [ 2 ]

0 голосов
/ 28 февраля 2019

Большое спасибо @obchardon:

Ниже приводится обновленная версия его кода, отвечающая моим требованиям:

function [] = slider_plot2()
x = 0 : 0.1 : 4*pi ;
y1 = sin (x) ;
y2 = cos (x) ;
  % Plot different plots according to slider location.
  S.fh = figure('position',[300 300 500 500],....
                'resize','off');    
  S.x = x;  %range.   
  S.y2 = y2  ;
  S.ax = axes('unit','pix',...
              'position',[30 80 460 410]);
  S.line2 = plot(S.x,y2,'r'); %sinus phase will move
  hold on
  S.line1 = plot(S.x,y1,'b');       
  S.sl = uicontrol('style','slide',...
                   'position',[20 10 260 30],...
                   'min',1,'max',length(x),'val',1,... %default phase = 0
                   'sliderstep',[1 1],...
                   'callback',{@call_S,S});  
function [] = call_S(varargin)
  % Callback for the phase slider.
  [h,S] = varargin{[1,3]};  % calling handle and data structure.
  currentPosition = floor(get(h,'value'))  ;
  ydata = S.y2(currentPosition:end) ;
  xdata = S.x(1:end-currentPosition+1)  ;
 set(S.line2,'xdata',xdata,'ydata',ydata) %set the new phase
 %set( S.line2,'ydata',ydata) %set the new phase
0 голосов
/ 28 февраля 2019

Вы можете построить свои 2 синуса / косинуса в функции, а затем использовать обратный вызов для обновления своего графика, здесь я обновляю фазу синусного графика в функции обратного вызова call_S:

function [] = slider_plot()
  % Plot different plots according to slider location.
  S.fh = figure('position',[300 300 300 300],....
                'resize','off');    
  S.x = 0:.01:4*pi;  %range.         
  S.ax = axes('unit','pix',...
              'position',[20 80 260 210]);
  S.sin = plot(S.x,sin(S.x),'r'); %sinus phase will move
  hold on
  S.cos = plot(S.x,cos(S.x),'b');       
  S.sl = uicontrol('style','slide',...
                   'position',[20 10 260 30],...
                   'min',0,'max',3*pi/2,'val',0,... %default phase = 0
                   'sliderstep',[0.1 0.1],...
                   'callback',{@call_S,S});  
function [] = call_S(varargin)
  % Callback for the phase slider.
  [h,S] = varargin{[1,3]};  % calling handle and data structure.
  set(S.sin,'ydata',sin(S.x + get(h,'value'))) %set the new phase

В этом случае я использую ползунок, но вы также можете использовать положение мыши для определения новой фазы.

Результат:

enter image description here

И вы можете перемещать ползунок для сдвига фазы:

enter image description here

...