xline with 1x3 array for the lable
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael
el 18 de Sept. de 2023
Comentada: Rik
el 19 de Sept. de 2023
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 comentarios
Dyuman Joshi
el 18 de Sept. de 2023
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.
Respuesta aceptada
Más respuestas (3)
Dyuman Joshi
el 18 de Sept. de 2023
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
%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' ''})
0 comentarios
Jerbin J
el 18 de Sept. de 2023
Editada: Jerbin J
el 18 de Sept. de 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 comentario
Dyuman Joshi
el 18 de Sept. de 2023
"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
el 18 de Sept. de 2023
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
b.field
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 comentarios
Rik
el 18 de Sept. de 2023
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"])
Rik
el 18 de Sept. de 2023
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])
Ver también
Categorías
Más información sobre Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!