0

I have a huge amount of data to plot, and thanks to:

x=matlab.lang.makeValidName(strcat(...)); 
assignin('base',x,I);

for each loop of process, where I calculate I, I assign the value of I (a vector) to the variable name I_Pnum1_Bnum2 where num1 is the value of P and num2 the value of B. So, at the end I have lots of I's for:

num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];

That's why, for each value of P, I want to plot (on the same graph) all I's for different B:

num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];
for i=1:length(num1)
    legend=[];
   figure(i)
   for j=1:length(num2)
       Y=matlab.lang.makeValidName(strcat('I_p',num2str(abs(num1(i))),'_B',num2str(double(num2(j)))));
       plot(V,eval(Y),'linewidth',2)
       hold on
       leg=strcat("B= ",num2str(b(j)));
       legend=[legend leg];
   end
   title(strcat("Caractéristiques I(V) @",num2str(p(i)),"dBm"))
   legend(legend);
end
clc;

The problem: I get

Function 'subsindex' is not defined for values of class 'string'.

and it is due to the line legend(legende), and I don't understand why, because the vector legende is well defined.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John
  • 303
  • 4
  • 13
  • 2
    You don't call `legend(legende)`, see your code. You call `legend(legend)`, which Georg's answer proofs is not a very smart idea. Don't call a sum `sum`, a mean `mean`, or a legend `legend`. Or, in general terms: **don't use the names of build-in functions for your variables**. – Adriaan Nov 20 '17 at 12:01
  • 1
    What is also a very bad practise is using *Dynamic Variable Names*, meaning those things you created using `assignin` and evaluate using the dreaded `eval`. Doing this is very error-prone and makes your code very, very slow, see [this answer of mine](https://stackoverflow.com/a/32467170/5211833) and references contained therein as to why. Just use a `cell` or `struct` which are purpose-build for this kind of thing without the huge downside of `eval`. – Adriaan Nov 20 '17 at 12:05

1 Answers1

1

The error occurs because of a conflict between your variable named legend and the build-in MATLAB function legend(). Rename your variable to e.g. leg1 then it should work as expected.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Georg W.
  • 1,292
  • 10
  • 27