Clear Filters
Clear Filters

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

1 view (last 30 days)
Maarten
Maarten on 23 Dec 2014
Commented: dpb on 4 Oct 2019
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 Comment
Maarten
Maarten on 23 Dec 2014
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

Sign in to comment.

Answers (1)

dpb
dpb on 23 Dec 2014
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 Comments
Daniel
Daniel on 3 Oct 2019
Edited: Daniel on 3 Oct 2019
Well done! This was the first solution that worked flawlessly for multiple axes in GUI after a week trying others.
dpb
dpb on 4 Oct 2019
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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!