how to label plot axes with index from a cell array of strings

I currently do this with the following function but I am looking for a way to refer to the labels with an index instead of using the switch statement. For example plotlabel(1) for xlabel, plotlabel(2) for ylabel, etc. Is there a property of the axis handle that allows me to do this or some other 'elegant' approach instead of the switch?
function LabelAxes(hax,root,subscripts)
axes(hax); % the labels will apply to this axis
for kaxis = 1:length(subscripts)
label = sprintf('%s_{%s}',root,subscripts{kaxis});
switch kaxis
case 1; xlabel(label);
case 2; ylabel(label);
case 3; zlabel(label);
end
end
An example of the use of the function is to label the axes as x_1, x_2, x_3 I would use the following code
subscripts = {'1','2','3'};
LabelAxes(gca,'x',subscripts)
edit: I am trying to avoid using xlabel, ylabel, zlabel. Having separate functions for each axis forces me to use the switch statement. I would like to replace that with a single statement where I refer to the label for each axis by a number index instead of with separate functions.

回答 (2 件)

Brendan Hamm
Brendan Hamm 2015 年 3 月 12 日

0 投票

label = strcat(root,subscripts);
xlabel(label{1})
ylabel(label{2})
zlabel(label{3})

4 件のコメント

Robert Alvarez
Robert Alvarez 2015 年 3 月 12 日
Sorry if my question was not clear. I am trying to avoid using xlabel, ylabel, zlabel. Having separate functions for each axis forces me to use the switch statement. I would like to replace that with a single statement where I refer to the label for each axis by a number index instead of with separate functions.
I am inserting this comment in the original question.
Brendan Hamm
Brendan Hamm 2015 年 3 月 12 日
編集済み: Brendan Hamm 2015 年 3 月 12 日
if true
% code
endThere is no way to refer to the axes-labels any other way.
If you want to avoid the switch though the above statement will work providing the length of subscripts is always 3. You could always pad label if it does not have length 3.
function LabelAxes(hax,root,subscripts)
% H1 line here
axes(hax); % the labels will apply to this axis
label = strcat([root,'_'],subscripts);
n = length(label)
if n < 3
label = [label cell(1,3-n)];
end
xlabel(label{1})
ylabel(label{2})
zlabel(label{3})
The only other way I can think of (not elegant at all is to make strings in a similar way and use the eval function.
AxLab = {'x','y','z'};
AxLab = strcat(AxLab,'label); % cell array of the function names
evalStr = strcat(AxLab,'(',label,')'; % Strings of function calls
eval(evalStr);
I would argue this is very unreadable and your solution is better.
Robert Alvarez
Robert Alvarez 2015 年 3 月 12 日
Thanks. I am afraid you are right. I have poked around in the axis handle fields and everything seems to be separated into x,y, and z.
Since I have written the function, I will use it.
I did try out your method of handling 2D plots and I'll be darned it does work. Apparently 2D plots have a z-axis anyway.
Brendan Hamm
Brendan Hamm 2015 年 3 月 13 日
I did consider one other way you could do this, as these are properties of the axes; 'XLabel' etc. We can use a method called "dynamic field names".
str = {'X','Y','Z'}
str = strcat(str,'Label');
ax = axes();
for i =1:3
ax.(str{i}) = label{i};
end
Here I assume that you are combining this with the method above. For more info on dynamic field names check out: http://blogs.mathworks.com/pick/2008/08/20/advanced-matlab-dynamic-field-names/

サインインしてコメントする。

Sara Hafeez
Sara Hafeez 2015 年 3 月 12 日

0 投票

Use label as label{1} like this will work in cell wit the label commands.

1 件のコメント

Robert Alvarez
Robert Alvarez 2015 年 3 月 12 日
Thanks for the answer but see my comment on Brendan Hamm's answer and the edit to the original question.

サインインしてコメントする。

カテゴリ

質問済み:

2015 年 3 月 12 日

コメント済み:

2015 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by