linkdata to structured data

9 visualizzazioni (ultimi 30 giorni)
Rafael Kübler
Rafael Kübler il 19 Lug 2016
Commentato: Adam il 19 Lug 2016
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

Risposta accettata

Adam
Adam il 19 Lug 2016
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 Commenti
Rafael Kübler
Rafael Kübler il 19 Lug 2016
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 il 19 Lug 2016
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.

Accedi per commentare.

Più risposte (1)

Stephen23
Stephen23 il 19 Lug 2016
Modificato: Stephen23 il 19 Lug 2016
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.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by