0

I want to use the following vector as loop vector in matlab:

year = ['JAN';'FEB';'MAR';'APR';'MAY';'JUN';'JUL';'AUG';'SEP';'OCT';'NOV';'DEC'];

The loop is

for i = year
    %Do something
    i
end

Unfortunately i is only one sign, and not three, i.e. I am expecting an output from the above as

JAN
FEB
MAR
APR
.
.
.

but I get only

J
F
M
A
M
.
.
.

How can I change that? Or should I rather use a look-up-table, and loop over a vector from 1 to 12?

1 Answer 1

1

What you got is a 12x3 matrix. To iterate over all rows you have to use matrix indexing:

for month=1:size(year,1)
   year(month,:)
end
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I thought that that is a 12x1-string-matrix.
There is no string class in matlab, there is only char. A very common error. Details here: mathworks.com/help/matlab/ref/strings.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.