Linkdata not refreshing graph

6 views (last 30 days)
Alex
Alex on 28 Aug 2024
Commented: Alex on 29 Aug 2024
I have code for a battery controler i'm making, and as part of that i want to display some live results as it continues running. The whole system runs in a repreating while loop which you can see below(have removed some code to make it legible).
while On = 'true'
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.
%% Initialise Display
Display = figure('Name','Controler Display','NumberTitle','off');
tiledlayout(2,1)
ax1 = nexttile;
plot(ax1,Display_timebase,Display_Power_cmd, "DisplayName","ESS Power Command (kW)")
title 'ESS input/output'
hold on
plot(ax1,Display_timebase,Display_Grid_Demand,"DisplayName","Grid Demand")
plot(ax1,Display_timebase,Display_Available_Power, "DisplayName","Available Power (kW)")
ylim([-300 320])
legend
xline(0)
hold off
ylabel("ESS Power in/output")
xlabel("Time")
xlim([-12 48])
ax2 = nexttile;
plot(ax2,Display_timebase,Display_ESS_SoC)
title 'SoC over time'
ylim([0 1.05])
ylabel("ESS SoC")
xlabel("Time")
xlim([-12 48])
xline(0)
linkdata(Display,'on')
drawnow
%%%%%%%%%%%
%% Additional Code
%%%%%%%%%%%
%% Update Display values
Display_Power_cmd = [Historical_power_cmd Navigator_Outputs.Power_cmd'];
Display_Grid_Demand = [Historical_Grid_Demand Navigator_Outputs.Grid_Demand'];
Display_Available_Power = [Historical_Available_Power Mapper_Outputs.Available_Power'];
Display_ESS_SoC = [Historical_SoC ESS_SoC_array'];
drawnow
end
I'm aware that being in a while loop suppresses figure updates, hence why i've included the drawnows but my figures still refuse to update. What am i doing wrong here?

Accepted Answer

Steven Lord
Steven Lord on 28 Aug 2024
As written, this is going to recreate the figure and the plots each time through the while loop. Do you want to move those ahead of the while loop and only update the variables in the loop?
Can you check that the data linkage was established correctly? From the first entry in the Tips section on the linkdata documentation page, "If linkdata cannot unambiguously identify data sources for a chart, then the chart will not synchronize with workspace variables. If you call linkdata and your chart does not update when you change a variable, then open the Linked Plot Data Sources dialog box by calling linkdata showdialog and manually link the chart to its data sources."
  1 Comment
Alex
Alex on 29 Aug 2024
Yes! This was the answer - the data was not correctly linked and so wasnt accepting the updates. Looking at the documentation for this it only seems to be able to do one data source per axis, is there a way to have multiple lines be linked?
ax1_Plot.XDataSource = 'Display_timebase';
ax1_Plot.YDataSource = 'Display_Power_cmd';
somethink akin to
ax1_Plot.YDataSource2 = 'Display_Power_cmd';

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!