why is this code not running?

2 visualizaciones (últimos 30 días)
Oyedeji
Oyedeji el 12 de Ag. de 2023
Comentada: Walter Roberson el 5 de Oct. de 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 comentario
C B
C B el 12 de Ag. de 2023
can you upload content of Pdispatch and TimeValue also what error are you facing ?

Iniciar sesión para comentar.

Respuestas (4)

VBBV
VBBV el 12 de Ag. de 2023
plot(TimeValue, Pdispatch(:, 1), 'Color','r', 'LineWidth', 2);
  3 comentarios
VBBV
VBBV el 12 de Ag. de 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 el 5 de Oct. de 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 .

Iniciar sesión para comentar.


C B
C B el 12 de Ag. de 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 el 12 de Ag. de 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 el 5 de Oct. de 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.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by