Hello Mathworks community,
I'm using the following loop to plot a graph for different scenarios:
figure
X = [1.182, 1.6, 1.7, 1.8, 1.9, 2]
NumberOfScenarios = [1, 2, 3]
Scenarios = string(NumberOfScenarios);
meanofscenarios = [0.8753 0.8685 0.8617 0.9099 0.9062 0.902 0.9044 0.9010 0.8975 0.9067 0.9030 0.8993 0.9141 0.9108 0.9076 0.9178 0.9147 0.9117]
for p = 1:numel(NumeroBESS)
plot(X, meanofscenarios(p : numel(NumberOfScenarios) : end), 'linewidth', 2); hold on
set(gca,'linewidth',2)
title('Title','FontName', 'Cambria', 'FontSize', 13); xlabel('Ratios DC/AC', 'FontName', 'Cambria', 'FontSize', 13); ylabel('Porcentaje de almacenamiento efectivo', 'FontName', 'Cambria', 'FontSize', 13)
legend('Scenario %d ', Scenarios(p)); hold on
end
I'm getting the graph I desire but I'd like the legend to display: Scenario 1, Scenario 2 and Scenario 3, but I'm not getting the expected output. I'm attaching an image of the plot I'm getting.
Could someone please give me a hand?
Than you
Santos

 Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 30 Mär. 2021

1 Stimme

hello
try this :
X = [1.182, 1.6, 1.7, 1.8, 1.9, 2]
NumberOfScenarios = [1, 2, 3]
Scenarios = string(NumberOfScenarios);
meanofscenarios = [0.8753 0.8685 0.8617 0.9099 0.9062 0.902 0.9044 0.9010 0.8975 0.9067 0.9030 0.8993 0.9141 0.9108 0.9076 0.9178 0.9147 0.9117]
figure(1),hold on
for p = 1:numel(NumeroBESS)
plot(X, meanofscenarios(p : numel(NumberOfScenarios) : end), 'linewidth', 2);
set(gca,'linewidth',2)
title('Title','FontName', 'Cambria', 'FontSize', 13);
xlabel('Ratios DC/AC', 'FontName', 'Cambria', 'FontSize', 13);
ylabel('Porcentaje de almacenamiento efectivo', 'FontName', 'Cambria', 'FontSize', 13)
legend_str{p} = (['Scenario ' num2str(Scenarios(p))]);
end
legend(legend_str);
hold off

4 Kommentare

Santos García Rosado
Santos García Rosado am 30 Mär. 2021
Thank's Mathieu! Now is working fine. I was wondering if perhaps you knew a way of doing it avoiding data convertion to cell?
I'm intending to use this in Simulink and I know that it doesn't like changing the format of data.
Thank's again!
hmm , does string array better fit your needs ?
X = [1.182, 1.6, 1.7, 1.8, 1.9, 2]
NumberOfScenarios = [1, 2, 3]
Scenarios = string(NumberOfScenarios);
meanofscenarios = [0.8753 0.8685 0.8617 0.9099 0.9062 0.902 0.9044 0.9010 0.8975 0.9067 0.9030 0.8993 0.9141 0.9108 0.9076 0.9178 0.9147 0.9117]
figure(1),hold on
for p = 1:numel(NumeroBESS)
plot(X, meanofscenarios(p : numel(NumberOfScenarios) : end), 'linewidth', 2);
set(gca,'linewidth',2)
title('Title','FontName', 'Cambria', 'FontSize', 13);
xlabel('Ratios DC/AC', 'FontName', 'Cambria', 'FontSize', 13);
ylabel('Porcentaje de almacenamiento efectivo', 'FontName', 'Cambria', 'FontSize', 13)
legend_str(p,:) = strcat('Scenario : ' , Scenarios(p));
end
legend(legend_str);
hold off
Santos García Rosado
Santos García Rosado am 30 Mär. 2021
Thank's Mathieu! This works better in my case. Thank's for the help!
Mathieu NOE
Mathieu NOE am 30 Mär. 2021
you're welcome !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by