xline with 1x3 array for the lable

3 ビュー (過去 30 日間)
Michael
Michael 2023 年 9 月 18 日
コメント済み: Rik 2023 年 9 月 19 日
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 Center および File ExchangeGraphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by