Creating a legend inside a loop, using sprintf

I am trying to make a legend for a plot with 'beta' number of curves. Basically, beta would be beta = [1 : 5] if I decided to have five curves, but it can be any number. I want to make a legend for that number of curves, and specifically do this by using a for loop (I know there are other ways that don't need for loops but I want to use a for loop for this one specifically). The code that I have (below) just gives me a legend for one curve "beta=1beta=2beta=3beta=4beta=5". I would need a legend for five curves. Basically the first curve would be beta = 1, the second curve would be beta = 2, and so on. Can someone help me?
for beta_val = beta
plot(time, y);
hold on
lgd = sprintf('beta = %0.0f', beta);
legend(lgd);
end

 採用された回答

Star Strider
Star Strider 2018 年 5 月 28 日

1 投票

Try something like this:
time = linspace(0, 2*pi); % Create Data
beta = 1:5; % Create Data
y = sin(time(:)*beta); % Create Data
for beta_val = beta
plot(time, y(:,beta_val));
hold on
lgd{beta_val} = sprintf('beta = %0.0f', beta(beta_val));
end
legend(lgd);

6 件のコメント

positron96
positron96 2018 年 5 月 28 日
編集済み: positron96 2018 年 5 月 28 日
So if I use your code, and append it to mine:
clear; clc;
alpha = 0.1; % degradation rate
time = [0 : 0.5 : 10]; % a vector for time ranging from 0 1o 10 seconds, in intervals of 0.5 second
beta = transpose([1 : 5]); % a vector for the production rate ranging from 1 to 5, in intervals of 1
y = zeros(5, 21); % initializing a matrix of zeros to store all mRNA expression levels (y) for each beta value (and each point in time); 5 beta values, 20 points in time
for time_point = 1 : length(time)
for beta_val = beta
y(beta_val, time_point) = beta ./ alpha .* (1 - exp(-alpha .* time(time_point)));
end
end
for beta_val = beta
plot(time, y(:,beta_val));
hold on
lgd{beta_val} = sprintf('beta = %0.0f', beta(beta_val));
end
legend(lgd);
I get the following error:
Error using plot Vectors must be the same length.
Error in testcode (line 23) plot(time, y(:,beta_val));
positron96
positron96 2018 年 5 月 28 日
Oh I figured it out! Thank you so much!
Star Strider
Star Strider 2018 年 5 月 28 日
You have row vectors for ‘y’. My code has column vectors. So with that and one other tweak, and using MATLAB’s vector and matrix vectorisation abilities to eliminate the first loop, we have:
alpha = 0.1; % degradation rate
time = [0 : 0.5 : 10]; % a vector for time ranging from 0 1o 10 seconds, in intervals of 0.5 second
beta = transpose([1 : 5]); % a vector for the production rate ranging from 1 to 5, in intervals of 1
y = beta ./ alpha .* (1 - exp(-alpha .* time));
for beta_val = 1:numel(beta)
plot(time, y(beta_val,:));
hold on
lgd{beta_val} = sprintf('beta = %0.0f', beta(beta_val));
end
legend(lgd, 'Location','N')
See if that does what you want.
positron96
positron96 2018 年 5 月 28 日
This actually works a ton better! Thanks so much!
Star Strider
Star Strider 2018 年 5 月 28 日
As always, my pleasure!
Jan
Jan 2018 年 5 月 28 日
If this works, you can omit the loop:
plot(time, y);
lgd{beta_val} = sprintfc('beta = %0.0f', beta);
legend(lgd, 'Location','N')

サインインしてコメントする。

その他の回答 (1 件)

Kai Åsvik
Kai Åsvik 2018 年 7 月 22 日

0 投票

I am getting this error message: "Function 'subsindex' is not defined for values of class 'cell'."

1 件のコメント

Jan
Jan 2018 年 7 月 22 日
@Kai: The problem is solved already and the readers cannot guess, which code causes your problem. Please open a new thread and include the code and the complete error message.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFit Postprocessing についてさらに検索

質問済み:

2018 年 5 月 27 日

コメント済み:

Jan
2018 年 7 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by