Linkdata using indexed data doesn't work

11 views (last 30 days)
Hello,
I'm plotting data from multiple files. The number of files is chosen by the user, so I don't know the number ahead of time. The variables that I'm trynig to plot are mxn dimension, where m is the number of files and n is the size of the variable in each file (e.g., time goes from 1:20 seconds in each file, if the user uploads 3 files, time is a 3x20 matrix. All the variables (time, powerFor, powerRef, powerDel) are defined in the workspace and sent to the function, as shown in the code below below. If the user chooses to upload more than 1 file, the linkdata doesn't work, since I guess it can't identify the data source unambigously. Is there a quick work around for this without having to change the enitre logic of the file upload such that it doesn't use matrices to store the data?
function CodeSnippet (time, powerFor, powerRef, powerDel)
for i = 1:numFiles
hold on
plot(time(i, :), powerFor(i, :),'LineWidth', 2); linkdata on;
plot(time(i, :), powerRef(i, :),'LineWidth', 2); linkdata on;
plot(time(i, :), powerDel(i, :),'LineWidth', 2); linkdata on;
end
end
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 22 Aug 2021
This is something that is not made obvious in the documentation :(
The documentation at https://www.mathworks.com/help/matlab/ref/matlab.graphics.internal.linkdata.html#mw_50ee8c7e-e6f5-49b2-a955-483ec60abd42 showing the example of linking complex data, implies that when you ask to plot an expression such as powerFor(i,:) then the expression is not recorded in the properties -- you only get automatic recording when the value to be plotted is an entire variable. But that example with Complex also shows that you can supply 'XDataSource' or 'YDataSource' options as a quoted string that designates the expression to execute to get the new data.
What is not talked about there or anywhere else obvious I have found, is a point I found buried in the description of XDataSource for plot():
Variable linked to XData, specified as a character vector or string containing a MATLAB workspace variable name. MATLAB evaluates the variable in the base workspace to generate the XData.
This means that all linking of variables fails, even of variables linked by just their plain name, except for going through the base workspace.
I tested this out and confirmed it :(
Note that in the case of your loop even if your data was in the base workspace, you would have to end up doing something like
plot(time(i, :), powerDel(i, :),'LineWidth', 2, 'XDataSource', "time("+i+",:)", 'YDataSource', "powerDel("+i+",:)")
  4 Comments
Walter Roberson
Walter Roberson on 23 Aug 2021
Unfortunately, MATLAB does not provide good tools for that.
There are some operations such as this, and some Simulink operations, that rely on the base workspace.
The ways to get something into the base workspace are:
  1. If you are using a script that is running outside of any function, then plain assignments go into the base workspace. Not useful within any function.
  2. use assignin()
  3. take advantage of the fact that if you create a callback that is specified as a character vector or scalar string(), then when the callback is triggered, the text entity will be executed in the context of the base workspace. This requires that you find a way to trigger the callback, such as creating a one-shot timer. This is undoubtedly a worse idea than using assignin()
I do not use data linking myself; when functions change the data, I code to have them push new values into the graphics objects as necessary.
Camila de Paula
Camila de Paula on 23 Aug 2021
Right now the assignin works fine so I might just stick with that. I think datalinking is a little easier in my case, since all I need is to time-shift many different plots based on user input, but nothing else abou the data ever changes, so re-plotting everything I think is unecessary. Thank you again for your help, I appreciate it!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!