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

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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 23 Dez. 2014

2 Stimmen

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 Kommentare

Maarten
Maarten am 23 Dez. 2014
Bearbeitet: Maarten am 23 Dez. 2014
I found the problem, since I was not using handles. I wrongly interpreted this code by thinkink ax1 was an array. I had to refer to the handles of an axes (the example says axis ).
Thanks, your comment helped my on thinking what I was doing wrong in this code!
Currently using this code and it works:
set(get(handles.ax1, 'Parent'), 'HandleVisibility', 'on'); %set handle visibility to on
axes(handles.ax1); %make ax1 the current axes
x1 = F;
y1 = S;
y2 = L;
y3 = D;
hl1 = line(x1,y1,'Color','r'); %plot a line in the current axes
hl2 = line(x1,y2,'Color','g');
%set(ax1,'XColor','r','YColor','r'); %set the axis colors of ax1
handles.ax2 = axes('Units', 'character'); %create a new axes and set units to be character
set(handles.ax2, 'Position',get(handles.ax1,'Position'),...
'YAxisLocation','right',...
'Color','none',...
'YColor','k'); %position the new axis on the earlier existing axis
hl3 = line(x1,y3,'Color','b','Parent',handles.ax2); %plot a line on the new axis
Daniel
Daniel am 3 Okt. 2019
Bearbeitet: Daniel am 3 Okt. 2019
Well done! This was the first solution that worked flawlessly for multiple axes in GUI after a week trying others.
dpb
dpb am 4 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 23 Dez. 2014

Kommentiert:

dpb
am 4 Okt. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by