2016-03-30 18 views
1

私は以下のコードを使用してシリアルポートからデータをプロットしています。私はプロットのための2つの軸を持っているので、どのようにこのプロットのための特定の軸を選択できますか? 同様の問題から、私は軸(handles.axes2);を使用することがわかりました。プログラムの開始時にプロットが宣言されているので、このコード行はどこに置くべきですか?プロットタイトルなどを指定する前に配置しようとしましたが、動作しません。Matlab GUIプロットする軸を選択

% Serial Data Logger 
% Yu Hin Hau 
% 7/9/2013 
% **CLOSE PLOT TO END SESSION 

clear 
clc 

%User Defined Properties 
serialPort = 'COM5';   % define COM port # 
plotTitle = 'Serial Data Log'; % plot title 
xLabel = 'Elapsed Time (s)'; % x-axis label 
yLabel = 'Data';    % y-axis label 
plotGrid = 'on';    % 'off' to turn off grid 
min = -1.5;      % set y-min 
max = 1.5;      % set y-max 
scrollWidth = 10;    % display period in plot, plot entire data log if <= 0 
delay = .01;     % make sure sample faster than resolution 

%Define Function Variables 
time = 0; 
data = 0; 
count = 0; 

%Set up Plot 
plotGraph = plot(time,data,'-mo',... 
       'LineWidth',1,... 
       'MarkerEdgeColor','k',... 
       'MarkerFaceColor',[.49 1 .63],... 
       'MarkerSize',2); 

title(plotTitle,'FontSize',25); 
xlabel(xLabel,'FontSize',15); 
ylabel(yLabel,'FontSize',15); 
axis([0 10 min max]); 
grid(plotGrid); 

%Open Serial COM Port 
s = serial(serialPort) 
disp('Close Plot to End Session'); 
fopen(s); 

tic 

while ishandle(plotGraph) %Loop when Plot is Active 

    dat = fscanf(s,'%f'); %Read Data from Serial as Float 

    if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct   
     count = count + 1;  
     time(count) = toc; %Extract Elapsed Time 
     data(count) = dat(1); %Extract 1st Data Element   

     %Set Axis according to Scroll Width 
     if(scrollWidth > 0) 
     set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth)); 
     axis([time(count)-scrollWidth time(count) min max]); 
     else 
     set(plotGraph,'XData',time,'YData',data); 
     axis([0 time(count) min max]); 
     end 

     %Allow MATLAB to Update Plot 
     pause(delay); 
    end 
end 

%Close Serial COM Port and Delete useless Variables 
fclose(s); 
clear count dat delay max min plotGraph plotGrid plotTitle s ... 
     scrollWidth serialPort xLabel yLabel; 


disp('Session Terminated...'); 
+0

'pause'ではなく' drawnow'を使って再描画を強制することができます – Suever

答えて

4

信頼性のプロットや操作を取得するためのトリックはplotまたは任意の他のグラフィックスオブジェクトを作成するときにParent parameterを使用して明示的に親を指定し、常ににあります。すべてのグラフィックスオブジェクトがこのパラメータをサポートしています。

hax = axes(); 
plot(x,y, 'Parent', hax); 

他の代替、@matlabguiによって示唆されるようにplotへの第1の入力として、親軸を指定することです。私が個人的にかかわらず、パラメータ値ペアとしてParentパラメータを使用することを好む

plot(hax, x, y); 

、その動作はすべてグラフィックスオブジェクトで一貫しているためです。

軸で操作する他の関数を使用する場合は、軸ハンドルも指定する必要があります。あなたは簡単に予期せず変更するgcaの値を引き起こし、あなたのプロットの真ん中に異なる軸をクリックすることができるユーザーとして対話型のGUIを扱っている場合

xlabel(hax, 'XLabel') 
ylabel(hax, 'YLabel') 
title(hax, 'This is a title') 
axis(hax, [0 0 1 1]) 
grid(hax, 'on') 
hold(hax, 'on') 

は、これは特に重要です。また、(axes(hax)を使用して)現在の軸を変更すると、ユーザーエクスペリエンスが低下する可能性があります。 gridtitleaxisxlabel:私はまたにあなたのコールにハンドル明示的な軸を追加することをお勧めし

plotGraph = plot(time,data,'-mo',... 
       'LineWidth',1,... 
       'MarkerEdgeColor','k',... 
       'MarkerFaceColor',[.49 1 .63],... 
       'MarkerSize',2, ... 
       'Parent', handles.axes2); 

あなたの特定のコードのサマリー

が、これはあなたの最初のplotコールを変更する伴うだろう、およびylabelを使用して、ターゲットが確実に目的の軸になるようにします。

+0

'plot'コマンドは親を第1引数として受け付けますので、' plot(hax、x、 y) ' – matlabgui

+0

@matlabguiはい、それは本当です。私はすべてのグラフィックスで一貫しているので、常にパラメータ/値として指定する方が好きです。 – Suever

+0

@TheBeginner 'xlabel'、' axis'などについては上記の私の例を見てください。特に 'axis'と' grid'の場合は、Axesハンドルを '* 'ではなく* first *入力として指定する必要があります。 Parent '、hax'形式です。 – Suever

関連する問題