linkdata to structured data

조회 수: 9 (최근 30일)
Rafael Kübler
Rafael Kübler 2016년 7월 19일
댓글: Adam 2016년 7월 19일
Hello everyone,
i've got to vectors for example.
a= 1:20
b=a.^2;
If i plot with
plot(a,b) i am able to linkdata. But if i put These vectors in a struct: For example struct.x=a and struct.y =b, i can plot with plot(struct.x, struct.y).
But now it's not possible to linkdata here. The error Shows up in the figure: "No graphics have data sources. Cannot link plot: fix it".
Is it possible to link data to data located in a structure?
Thank you for your answers.
Rafael

채택된 답변

Adam
Adam 2016년 7월 19일
You can set the 'XDataSource' and 'YDataSource' of the line object to e.g myStruct.a and myStruct.b (never call a struct 'struct'!) either programmatically or via clicking on the line and editing properties.
  댓글 수: 2
Rafael Kübler
Rafael Kübler 2016년 7월 19일
Thank you alot. This helped me.
Now i want to do the same for a surf or a pcolor. But there is no Propertie 'XDataSource', YDataSource' or 'ZDataSource' in surf or pcolor.
Is there an other way to set the sources here?
Adam
Adam 2016년 7월 19일
As far as I am aware you cannot link data in a surf plot.
You can do the equivalent just with programming though which is what I tend to do even for line plots - just update the 'XData', 'YData', 'ZData', 'CData' and any other appropriate properties of your line or surf object in response to data changing. Usually I would do this using listeners as updating a plot in the same function(s) where the data changes is an ugly solution.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2016년 7월 19일
편집: Stephen23 2016년 7월 19일
Solution
Yes it is possible, but you will have to either click the "Edit.." or "fix it" link in the figure, or by specifying those variables when the plot function is called:
>> S.a = 1:20;
>> S.b = S.a.^2;
>> plot(S.a,S.b,'XDataSource','S.a','YDataSource','S.b')
>> linkdata on
Explanation
The linkdata help states that it "compares variables in the current (base or function caller) workspace with the XData, YData, and ZData properties of graphs in the affected figure to try to match them. When a match is found, the appropriate XDataSource, YDataSource and/or ZDataSource for the graph are set to strings that name the matching variables."
Note that it does not recursively look inside those variables, it will only check and match simple numeric variables. But by providing S.a and S.b these are no longer variables but expressions containing the required data. MATLAB cannot match these to the structure fields because it does not do a recursive search inside any structures or cell arrays.
In any case, the most reliable way to use linkdata is to always specify the data source when calling the plot function, this means MATLAB does not need to search and match the variables in the workspace.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by