The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: saving strings in a for loop
Replies: 2   Last Post: Mar 20, 2014 5:11 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 2
Registered: 3/20/14
Re: saving strings in a for loop
Posted: Mar 20, 2014 5:11 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


I didn't know the {} existed, but it was exactly what I was looking for.


"someone" wrote in message <lgfip8$ah8$>...
> "Stephen " <> wrote in message <lgfgph$4ee$>...
> > I'm working on a problem in which I have written a script to extract the first letter of each word in a sentence. These collected letters form a new word. I have ten sentences (and therefore ten words that I need to save to make a new sentence). I put a for loop around the extraction script, but I can't figure out how to save the word decoded from each sentence. my variable 'word' is simply overwritten every time I go through the loop.
> >
> > %a-j are the individual sentences
> > % What I've done is write the sentences, and concatenate them into a matrix. I look for spaces in each row (sentence) and concatenate the first letter and every letter following a space to form 'word.'
> >
> > I want to save each word in an array and print it out so it reads like a sentence.
> >
> > z=char (a,b,c,d,e,f,g,h,i,j)
> >
> > [r,c]=size(z);
> > for n=1:r;
> > x=deblank(z(n,:));
> > s=find(x==' ');
> > word = [x(1), x(s+1)]
> > end

> I don't know how the above code matches your description, but yes you are overwritting "word" each time through the loop. You might try changing:
> word = [x(1), x(s+1)]
> to something like:
> word{n} = [x(1), x(s+1)] % this makes word a cell array
> or
> word = [word x(1), x(s+1)] % this appends whatever to word

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.