why is this code not running?

2 views (last 30 days)
Oyedeji
Oyedeji on 12 Aug 2023
Commented: Walter Roberson on 5 Oct 2023
% Create plots for generator dispatch
figure;
subplot(2, 1, 1);
plot(TimeValue, Pdispatch(:, 1), 'r', 'LineWidth', 2); % Solar
hold on;
plot(TimeValue, Pdispatch(:, 2), 'g', 'LineWidth', 2); % Wind
plot(TimeValue, Pdispatch(:, 3), 'k', 'LineWidth', 2); % Coal
plot(TimeValue, Pdispatch(:, 4), 'b', 'LineWidth', 2); % Gas
plot(TimeValue, Pdispatch(:, 5), 'c', 'LineWidth', 2); % Hydro
title('Generator Dispatch Over Time');
xlabel('Time (hours)');
ylabel('Power Dispatch (MW)');
legend('Solar', 'Wind', 'Coal', 'Gas', 'Hydro');
grid on;
  1 Comment
C B
C B on 12 Aug 2023
can you upload content of Pdispatch and TimeValue also what error are you facing ?

Sign in to comment.

Answers (4)

VBBV
VBBV on 12 Aug 2023
plot(TimeValue, Pdispatch(:, 1), 'Color','r', 'LineWidth', 2);
  3 Comments
VBBV
VBBV on 12 Aug 2023

Probably there must be categorical or other form of data for TimeValue matrix.

See this link for more info on how to plot then alike.

https://in.mathworks.com/help/matlab/ref/datetime.html https://in.mathworks.com/help/matlab/categorical-arrays.html

Walter Roberson
Walter Roberson on 5 Oct 2023
plot(TimeValue, Pdispatch(:, 1), 'r', 'LineWidth', 2); % Solar
is fine. plot() permits a positional linespec after x y pairs, and color letters are permitted in linespec . You would need 'Color' if you were setting a color by other methds such as RGB .

Sign in to comment.


C B
C B on 12 Aug 2023
% Time array (24 hours)
TimeValue = 1:24;
% random data
SolarDispatch = 80 + 5*randn(1, 24);
WindDispatch = 50 + 6*randn(1, 24);
CoalDispatch = 100 + 7*randn(1, 24);
GasDispatch = 20 + 8*randn(1, 24);
HydroDispatch = 30 + 9*randn(1, 24);
Pdispatch = [SolarDispatch; WindDispatch; CoalDispatch; GasDispatch; HydroDispatch]';
I have taken random data like above , but your mat file can be different. please let me know if you are still facing issue. code seems to be ok.
figure;
subplot(2, 1, 1);
plot(TimeValue, Pdispatch(:, 1), 'r', 'LineWidth', 2); % Solar
hold on;
plot(TimeValue, Pdispatch(:, 2), 'g', 'LineWidth', 2); % Wind
plot(TimeValue, Pdispatch(:, 3), 'k', 'LineWidth', 2); % Coal
plot(TimeValue, Pdispatch(:, 4), 'b', 'LineWidth', 2); % Gas
plot(TimeValue, Pdispatch(:, 5), 'c', 'LineWidth', 2); % Hydro
title('Generator Dispatch Over Time');
xlabel('Time (hours)');
ylabel('Power Dispatch (MW)');
legend('Solar', 'Wind', 'Coal', 'Gas', 'Hydro');
grid on;

Star Strider
Star Strider on 12 Aug 2023
What does ‘not running’ mean? What is not working correctly?
When I supply values for ‘TimeValue’ and ‘Pdispatch’ it runs without error —
TimeValue = 0:23;
Pdispatch = rand(24, 5);
figure;
subplot(2, 1, 1);
plot(TimeValue, Pdispatch(:, 1), 'r', 'LineWidth', 2); % Solar
hold on;
plot(TimeValue, Pdispatch(:, 2), 'g', 'LineWidth', 2); % Wind
plot(TimeValue, Pdispatch(:, 3), 'k', 'LineWidth', 2); % Coal
plot(TimeValue, Pdispatch(:, 4), 'b', 'LineWidth', 2); % Gas
plot(TimeValue, Pdispatch(:, 5), 'c', 'LineWidth', 2); % Hydro
title('Generator Dispatch Over Time');
xlabel('Time (hours)');
ylabel('Power Dispatch (MW)');
legend('Solar', 'Wind', 'Coal', 'Gas', 'Hydro');
grid on;
.

SAMWESLIN S
SAMWESLIN S on 5 Oct 2023
% Create plots for generator dispatch
figure;
% Subplot for Power Dispatch
subplot(2, 1, 1);
% Plot different generators
plot(TimeValue, Pdispatch(:, 1), 'r', 'LineWidth', 2); % Solar
Unrecognized function or variable 'TimeValue'.
hold on;
plot(TimeValue, Pdispatch(:, 2), 'g', 'LineWidth', 2); % Wind
plot(TimeValue, Pdispatch(:, 3), 'k', 'LineWidth', 2); % Coal
plot(TimeValue, Pdispatch(:, 4), 'b', 'LineWidth', 2); % Gas
plot(TimeValue, Pdispatch(:, 5), 'c', 'LineWidth', 2); % Hydro
% Title and labels
title('Generator Dispatch Over Time');
xlabel('Time (hours)');
ylabel('Power Dispatch (MW)');
% Legend and grid
legend('Solar', 'Wind', 'Coal', 'Gas', 'Hydro');
grid on;
Make sure that your TimeValue and Pdispatch variables are correctly defined and contain the necessary data, and this code should work as intended in a MATLAB environment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!