How can I resolve this error?

I have written this code:
figure
name = 'cap_';
vector = {'T0' 'T1' 'T2' 'T3' 'T4' 'T5' 'T6' 'T7' T8' 'T9'};
for i=1:length(vector)
name_1 = strcat(name, vector(i));
name_1 = char(name_1);
plot(x, sscanf(name_1, '%s'), 'y');
hold on
name_1 = [];
end
When I run this code I receive this message error: There is no cap_T1 property on the Line class
If I use:
plot(x, sscanf(name_1, '%s')) I receive this other message: Error in color/linetype argument
Help me please.
Thanks

3 件のコメント

Walter Roberson
Walter Roberson 2017 年 9 月 30 日
編集済み: Walter Roberson 2017 年 9 月 30 日
What would you like the code to do?
Gianmarco Polizia
Gianmarco Polizia 2017 年 9 月 30 日
編集済み: Gianmarco Polizia 2017 年 9 月 30 日
Hello Walter thanks for attention payed to my question
I would like that the for loop substitutes these code lines:
plot(frequency_1, cap_1)
hold on
plot(frequency_2, cap_2)
.
.
.
plot(frequency_10, cap_10)
Walter Roberson
Walter Roberson 2017 年 9 月 30 日
... Don't do that!

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

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 9 月 30 日

0 投票

5 件のコメント

Gianmarco Polizia
Gianmarco Polizia 2017 年 10 月 1 日
編集済み: Gianmarco Polizia 2017 年 10 月 1 日
Writing this: plot(x,sscanf(y)) is like to use eval()?
Jan
Jan 2017 年 10 月 1 日
@Gianmarco: sscanf does not do, what you assume. It does not evaluate a variable with the name given in the string, but parses the string itself and tries to convert it to a number:
s = '89.81'
v = sscanf(s, '%f');
Now s=89.81. What you want to do instead is evaluate a string as a variable, and as explained in the links posted by Walter, this is a shot in your knee. Instead of creating:
vector = {'T0' 'T1' 'T2' 'T3' 'T4' 'T5' 'T6' 'T7' T8' 'T9'};
use:
vector = {T0, T1, T2, T3, T4, T5, T6, T7, T8, T9};
Or even better do not create the set of variables at all, but store them in an array directly. Then accessing them in a loop is easy by using an index.
Gianmarco Polizia
Gianmarco Polizia 2017 年 10 月 1 日
@Jan I don't understand the difference between do not create the set of variables at all and store them in an array directly.
In the first case, how is the code? And how is the code in the second case?
Thanks
Walter Roberson
Walter Roberson 2017 年 10 月 1 日
It is better to avoid creating numbered variables and to create indexed variables instead. But if you do for some reason have numbered variables then instead of creating variable names at run-time, it is better to take the one-time hit of putting the variables together into an array and after that index the array.
frequencies = {frequency_1, frequency_2, frequency_3, frequency_4, frequency_5, frequency_6, frequency_7, frequency_8, frequency_9, frequency_10};
caps = {cap_1, cap_2, cap_3, cap_4, cap_5, cap_6, cap_7, cap_8, cap_9, cap_10};
names = cellstr( num2str((1:10).', 'line #%d') );
for K = 1 : length(frequencies)
plot(frequencies{K}, caps{K});
hold on
end
legend(names);

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

カテゴリ

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

製品

タグ

質問済み:

2017 年 9 月 30 日

コメント済み:

2017 年 10 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by