Format Issues - will not accept a character or a double

1 次查看(过去 30 天)
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);

回答(2 个)

Thorsten
Thorsten 2015-9-25
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 个评论
Chameleon17
Chameleon17 2015-9-25
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 2015-9-25
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

请先登录,再进行评论。


Guillaume
Guillaume 2015-9-25
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by