Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Colon operator

Asked by Elizabeth

Elizabeth (view profile)

on 13 Apr 2012

Hi! I keep getting the error "For colon operator with char operands, first and last operands must be char". with the line "for i=1:block", where block=9.

The code I'm using is as follows:

function reach_reward2(subj_num,name,block)
filename = 'C:\Users\Elizabeth\Documents\MATLAB Files\Experiment Data\' subj_num];
cd (filename)
dat=['RT1_' name '.dat'];
file = [subj_num '_' name];
for i=1:block %block
  num  = ['1' '2' '3' '4' '5' '6' '7' '8' '9'];
  subj = ['RT1_' name '_' num(i) '.mov'];
  Data = movload(subj);
  combined_data = [];
and so on..

I'm confused as to what I'm doing wrong?


Sean de Wolski

Sean de Wolski (view profile)

on 13 Apr 2012

what is block?

Walter Roberson

Walter Roberson (view profile)

on 13 Apr 2012

If block was a character you would get that error.


Elizabeth (view profile)


No products are associated with this question.

2 Answers

Answer by Vaibhav

Vaibhav (view profile)

on 13 Apr 2012

You have to initialize block as a number such as 20 or 30 or whatever.



Vaibhav (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 13 Apr 2012

The error message looks like block is a string - a char vector. How do you call this function?

BTW, this is not efficient:

num  = ['1' '2' '3' '4' '5' '6' '7' '8' '9'];


num  = '123456789';

But the repeated definition inside the loop wastes time also. It is a good programming practize to move all repeated operations out of the loop.

Most likely sprintf('%d', i) is even better to create the number as string.


Jan Simon

Jan Simon (view profile)

Contact us