"For colon operator with char operands, first and last operands must be char." error?

36 ビュー (過去 30 日間)
laurie
laurie 2015 年 2 月 25 日
コメント済み: laurie 2015 年 2 月 25 日
for n=1:1:4
MAT((a:(a+b)),n)=1;
end
a and b are integer numbers that change on each iteration of n
The code is meant to set some zeros in a zero matrix to 1
On the 2nd iteration, i.e. n=2, I keep getting the following error:
"For colon operator with char operands, first and last operands must be char."
  1 件のコメント
laurie
laurie 2015 年 2 月 25 日
編集済み: laurie 2015 年 2 月 25 日
% OPTIONS.popsize=4;
% horizon = 6;
% num_gen = 4;
%Population(pop).chrom = structure 'Population' with a cell 'chrom'
% chrom = 1x4 matrix
%Data is also a struct
for pop=1:1:OPTIONS.popsize
Population(pop).chrom2 = zeros(horizon,num_gen);
for n=1:1:num_gen
a=Population(pop).chrom(1,n);
b=Data.tend(n)-Data.tstart(n)-1;
c=a:(a+b)
%Population(pop).chrom2((a:(a+b)),n)=1;
Population(pop).chrom2(c,n)=1;
end
end

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

採用された回答

Guillaume
Guillaume 2015 年 2 月 25 日
According to the error message, a and b are integer numbers is not true. I suspect that a is actually of type char and b may be integer, which would result in a+b being integer.
Without knowing more about a and b, it's hard to help you, so post the code that generate them.
You can check the type of a and b when you get the error by writing
dbstop if error
at the comment prompt before running your loop / program. Matlab will break into the debugger when it encounters the error. At which point you can issue
whos a b
to see their type.
  4 件のコメント
Guillaume
Guillaume 2015 年 2 月 25 日
What is the output of
chrom = Population(pop).chrom
whos chrom
when the error occurs? Because that's the source of your problem. Somewhere, you've got a string.
laurie
laurie 2015 年 2 月 25 日
The output was a char, which came from a previous function I created.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVector Volume Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by