xline with 1x3 array for the lable

Hey,
I have a struct of the following
vertical = struct ( ...
vertLine = 0,
vertLineText = strings(1,1) ...
);
which should be used to draw vertical lines and attach a lable to them.
I dynamically expand it to maybe:
vertical.vertLine = [0,1688,3134]
vertical.vertLineText = 1x3 string
But how to get it working.
xline([vertical.vertLine], '--b', [vertical.vertLineText]);
doesn't.

2 Comments

This snippet of code works fine here.
vertical.vertLine = [500,1688,3134];
vertical.vertLineText = ["X", "Y", "Z"];
figure
xline(vertical.vertLine, '--b', [vertical.vertLineText]);
xlim([0 3500])
Though I doubt that this is your whole code.
Please copy and paste or attach your whole code.
You are right.
The example works lick a charme.
But please use the following code:
vertical = struct ( ...
vertLine = 0, ...
vertLineText = strings(1,1) ...
);
testData = [1688,3134];
testText = ["X", "Y"];
for i=1:2
vertical.vertLine(end + 1) = testData(i);
vertical.vertLineText(end + 1) = testText(i);
end
figure
xline(vertical.vertLine, '--b', [vertical.vertLineText]);
Error using xline
Invalid parameter/value pair arguments.
xlim([0 3500])
which shows my original problem.

Sign in to comment.

 Accepted Answer

Hey,
finally I used the following approach which sums a a few ideas collected in the forum:
vertical = struct ( ...
'vertLine', {}, ...
'vertLineText', {} ...
);
testData = [1688,3134];
testText = ["X", "Y"];
for i=1:2
vertical(end + 1).vertLine = testData(i);
vertical(end).vertLineText = testText(i);
end
figure
xline([vertical.vertLine], '--b', [vertical.vertLineText]);
xlim([0 3500])
Thank you all for the very good and very fast input and help.

1 Comment

You're welcome. If you any of the answers helped you, please consider giving them an upvote.

Sign in to comment.

More Answers (3)

A Note - Dynamically growing arrays is not a good coding practice. Consider Preallocation. There is not much noticeable effect with smaller arrays, but for larger arrays there will be a significant difference in efficiency with preallocation.
As for the problem you are facing - It seems when providing the 1st input as an array (of numeric values) to xline() and yline(), MATLAB expects the corresponding labels to have no empty values/elements.
Solution - Plot individually
testData = [1688,3134];
testText = ["X", "Y"];
vertical.vertLine = [500 testData];
vertical.vertLineText = [strings(1,1) testText];
n = numel(vertical.vertLine);
vertical = struct with fields:
vertLine: [500 1688 3134] vertLineText: ["" "X" "Y"]
vertical
%Plotting individually
figure
for k=1:n
xline(vertical.vertLine(k), '--b',vertical.vertLineText(k))
end
xlim([0 3500])
%Plotting together
figure
xlim([0.5 3.5])
%Error
%Works for the 1st 2 lines but not for the 3rd line with empty character vector in cell array
xline([1 2 3], '--b',{'yo' 'sup' ''})
Error using xline
Invalid parameter/value pair arguments.
Jerbin J
Jerbin J on 18 Sep 2023
Edited: Jerbin J on 18 Sep 2023
vertical = struct(...
'vertLine', [], ...
'vertLineText', strings(1, 0) ...
);
testData = [1688, 3134];
testText = ["X", "Y"];
figure
for i = 1:length(testData)
xline(testData(i), '--b');
vertical.vertLine(end + 1) = testData(i);
vertical.vertLineText(end + 1) = testText(i);
end
xlim([0 3500])
% Labels
text(vertical.vertLine, zeros(size(vertical.vertLine)), vertical.vertLineText, 'VerticalAlignment', 'bottom');

1 Comment

"The 'xline' function expects a single numeric value or an array of numeric values, but you are trying to pass both values and labels as arguments in a way that it doesn't support."
Are you sure?
testData = [1688, 3134];
testText = ["X", "Y"];
figure
xline(testData,'r:',testText)

Sign in to comment.

The difference is having a scalar struct with an array field, or a struct array with scalar fields. Dot indexing the latter will produce a comma separated list.
a.field = [1 2 3];
for n=1:3,b(n).field = n;end
a.field
ans = 1×3
1 2 3
b.field
ans = 1
ans = 2
ans = 3
The second option will make the function intrepet the input a multiple separate arguments, which is not what you meant to do. Since Matlab does what you tell it, not what you want it to do, the error occurs.
You can either loop through the struct elements, or concatenate with [].

3 Comments

Concatenating gives an error as well
testData = [1688,3134];
testText = ["X", "Y"];
vertical.vertLine = [500 testData];
vertical.vertLineText = [strings(1,1) testText];
vertical
vertical = struct with fields:
vertLine: [500 1688 3134] vertLineText: ["" "X" "Y"]
vec = cellstr(vertical.vertLineText)
vec = 1×3 cell array
{0×0 char} {'X'} {'Y'}
figure
%Concatenating
xline([vertical.vertLine], '--b',[vertical.vertLineText])
Error using xline
Invalid parameter/value pair arguments.
xlim([0 3500])
%This gives error as well
figure
%Cell array of character vectors
xline([vertical.vertLine], '--b',vec)
xlim([0 3500])
Interesting. It gets confused by an empty element. Other than that, I don't see what is the syntactic difference between [] on a CSL and using the array directly.
xline([500 1688 3134],'--b',["X","Y","Z"])
xline([500 1688 3134],'--b',["","Y","Z"])
Error using xline
Invalid parameter/value pair arguments.
So it is the pre-allocation. In that case we can just remove the first element and we're done:
vertical = struct ( ...
vertLine = 0, ...
vertLineText = strings(1,1) ...
);
vertical.vertLine(1)=[];
vertical.vertLineText(1)=[];
testData = [1688,3134];
testText = ["X", "Y"];
for i=1:2
vertical.vertLine(end + 1) = testData(i);
vertical.vertLineText(end + 1) = testText(i);
end
figure
xline(vertical.vertLine, '--b', [vertical.vertLineText]);
xlim([0 3500])

Sign in to comment.

Categories

Tags

Asked:

on 18 Sep 2023

Commented:

Rik
on 19 Sep 2023

Community Treasure Hunt

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

Start Hunting!