xline with 1x3 array for the lable

조회 수: 12 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by