linkdata to structured data

7 visualizaciones (últimos 30 días)
Rafael Kübler
Rafael Kübler el 19 de Jul. de 2016
Comentada: Adam el 19 de Jul. de 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

Respuesta aceptada

Adam
Adam el 19 de Jul. de 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 comentarios
Rafael Kübler
Rafael Kübler el 19 de Jul. de 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 el 19 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 19 de Jul. de 2016
Editada: Stephen23 el 19 de Jul. de 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.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by