xline with 1x3 array for the lable

2 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-9-18
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.
Michael
Michael 2023-9-18
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.

请先登录,再进行评论。

采纳的回答

Michael
Michael 2023-9-19
编辑:Michael 2023-9-19
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 个评论
Rik
Rik 2023-9-19
You're welcome. If you any of the answers helped you, please consider giving them an upvote.

请先登录,再进行评论。

更多回答(3 个)

Dyuman Joshi
Dyuman Joshi 2023-9-18
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 2023-9-18
编辑:Jerbin J 2023-9-18
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-9-18
"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)

请先登录,再进行评论。


Rik
Rik 2023-9-18
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 个评论
Rik
Rik 2023-9-18
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.
Rik
Rik 2023-9-18
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])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by