Format Issues - will not accept a character or a double

3 views (last 30 days)
Hi,
I'm having another really basic problem.
I want to use a different source for each plant, but it really doesn't like the format.
I get an error message at Xsource saying undefined function X_src_ for function of double or character if I change it. Am I missing something quite obvious?
Any help/advise is very much appreciated!
for plant = 1:5
Xsource = x_src_(num2str(plant));
Ysource = y_src_(num2str(plant)) ;
X_shft = X_rot - x_src_(Xsource);
Y_shft = Y_rot - y_src_(Ysource);
[i] = find(X_shft > 0);

Answers (2)

Thorsten
Thorsten on 25 Sep 2015
It would be helpful to know 1) what you want to achieve in your code and 2) how are the variables defined, like x_src_c, X_rot, Y_rot etc.
In particular, what is x_src_? You can check using
whos x_src_
Regardless of what x_src_ actually is, it you try to index into x_src_ using a string, e.g., '1' by using num2str(plant)). That seems strange to me. What do you want to achieve in this line of code?
  2 Comments
Chameleon17
Chameleon17 on 25 Sep 2015
Hi, thanks for the answer. I've abandoned the string bit now, it doesn't make sense. I thought it might have been the problem with the double issue I suppose.
I'm just trying to use different locations for each of my plants.
for plants = 1:5
x_src_1 = 0; y_src_1 = 0; x_src_2 = 10000; y_src_2 = 10000; x_src_3 = -10000; y_src_3 = -10000; x_src_4 = 20000; y_src_4 = 20000; x_src_5 = -20000; x_src_5 = -20000;
Xsource = x_src_(plants); Ysource = y_src_(plants);
Thorsten
Thorsten on 25 Sep 2015
Do you mean like this?
for plants = -20000:10000:20000
Xsource = plants; Ysource = plants;
% further code here
end
or if you insists on the order of the plants
for plants = [0 10000 -10000 20000 -20000]
Xsource = plants; Ysource = plants;
% further code here
end

Sign in to comment.


Guillaume
Guillaume on 25 Sep 2015
From your comment it looks like you're trying to index different variable names depending on a condition. The answer to that is very simple: DO NOT DO THAT. In matlab or any other language.
Create an array and index into this array
X_src = [0, 10000, -10000, 20000, -20000];
Y_src = [0, 10000, -10000, 20000, -20000];
for plant = 1:5
Xsource = X_src(plant);
Ysource = Y_src(plant);
%...
end
Using numbered variables is very poor practice. There are many posts on this forum and a lengthy explanation in the faq detailing why.

Categories

Find more on Control System Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!