linkdata to structured data
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!