Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:28:15+00:00 2026-05-31T17:28:15+00:00

I want to create a loop that will iterate over several strings, but unable

  • 0

I want to create a loop that will iterate over several strings, but unable to do so in Matlab.

What works is:

for i=1:3
  if (i==1)
    b='cow';
  elseif (i==2)
    b='dog';
  else
    b='cat';
  end

  disp(b);
end

and the result is:

cow
dog
cat

But what I want is something more elegant that will look like:

for i=['cow','dog','cat']
  disp (i);
end

and give the same result.

Is there an option in Matlab to do this?

ADDITION:

I need the words as strings later on to use and not just to display (the disp was just as an example). I’ve tried to use cell arrays in my real program:

clear all;
close all;
clc;

global fp_a
global TEST_TYPE
global SHADE_METHODE

for fp_a=11:24
for shade={'full','fast'}
    SHADE_METHODE=shade(1);
    for test={'bunny','city'}
        TEST_MODE=test(1);
        fprintf ('fp_a:%d | test: %s | shade: %s',fp_a,TEST_TYPE,SHADE_METHODE);
        ray_tracing;
    end
end
end

It doesn’t work as the values stay as cells and not strings I get the error message:

??? Error using ==> fprintf Function is not defined for ‘cell’ inputs.

*-I don’t really need the fprintf I just use it to check that the values are correct.

**-ray_tracing is my code that uses the values of the strings

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:28:16+00:00Added an answer on May 31, 2026 at 5:28 pm

    Or you can do:

    for i={'cow','dog','cat'}
       disp(i{1})
    end
    

    Result:

    cow
    dog
    cat
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a queue of Strings that will have many identical elements
I want to create a function like a for loop where I will ideally
having a small problem. I'm trying to create a loop that will create an
I'm trying to create a function that will loop through an array of various
I'm trying to create an export Excel/CSV function that will iterate through a custom
I want to create a splash screen that will then move to the login/register
I want to create a page with, for example, static text and loop that
let's say that I want to create a new string during a for loop,
I want to create a splash screen that will then move to the login/register
Is it possible to create an animation in GIMP that will loop only certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.