Format Issues - will not accept a character or a double

조회 수: 1 (최근 30일)
Chameleon17
Chameleon17 2015년 9월 25일
댓글: Thorsten 2015년 9월 25일
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