필터 지우기
필터 지우기

Plot multiple axis in GUI (2 y-axis and 1 x-axis)

조회 수: 2 (최근 30일)
Maarten
Maarten 2014년 12월 23일
댓글: dpb 2019년 10월 4일
I am having difficulties trying to plot 3 lines in one axes with two y-axis. I import an Excel file with 4 columns:
G=xlsread*'Test1.xlsx','Sheet1','A3:D18'); %loading the Excel file
F = G(:,1); %these values represent the x-axis
S = G(:,2); %these values are for y-axis 1
L = G(:,3); %these values are for y-axis 1
D = G(:,4); %these values are for y-axis 2
I found this link to plot multiple axis into a GUI, but it does not even work in Matlab r2014b: http://www.mathworks.com/matlabcentral/answers/100583-how-do-i-create-multiple-axis-objects-in-a-gui-using-matlab-7-7-r2008b
Maybe anyone can help me with this.
  댓글 수: 1
Maarten
Maarten 2014년 12월 23일
I copied the code and get an error as shown below:
Error using set
Conversion to double from cell is not possible.
Error in VibrationModel>G755_Callback (line 688)
set(get(ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
The code I used:
G=xlsread('Test1.xlsx','Sheet1','A3:D18');
F = G(:,1);
S = G(:,2);
%L = G(:,3);
D = G(:,4);
ax1 = findall(0, 'type', 'axes'); %assumes the axis inside the GUI is the only axis
set(get(ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(ax1); %make ax1 the current axis
x1 = F;
y1 = S;
x2 = F;
y2 = D;
hl1 = line(x1,y1,'Color','r'); %plot a line in the current axis
set(ax1,'XColor','r','YColor','r'); %set the axis colors of ax1
ax2 = axes('Units', 'character'); %create a new axis and set units to be character
set(ax2, 'Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k'); %position the new axis on the earlier existing axis
hl2 = line(x2,y2,'Color','k','Parent',ax2); %plot a line on the new axis

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2014년 12월 23일
The key is in the error message "Conversion to double from cell..." combined with the subject line of multiple axes and the comment on the line in question _"%assumes the axis inside the GUI is the only axis".
Says ax1 is a cell (or perhaps a cell array if the assumption of only a single axes as noted in the comment isn't true).
If it is only one element returned, then use
ax1 = cell2mat(findall(0, 'type', 'axes'));
to convert to the double.
If there are more than the one axes objects, then you've got more work to do to find the one of interest, specifically.
  댓글 수: 3
Daniel
Daniel 2019년 10월 3일
편집: Daniel 2019년 10월 3일
Well done! This was the first solution that worked flawlessly for multiple axes in GUI after a week trying others.
dpb
dpb 2019년 10월 4일
I don't do GUIs but it's unclear to me why yyaxis wouldn't work there just as well as not...
The typical problem in GUIs is ensuring access to the necessary data in the callback functions.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by