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

Thread Subject:
Appending characters to a string array in a for loop?

Subject: Appending characters to a string array in a for loop?

From: M.E.L.

Date: 22 Feb, 2012 22:34:12

Message: 1 of 4

Here's the short story:

I want to create a string array in a for loop, such that I can vary the number of inputs in the array. I want to save a large data set as a matlab file using:

save('data.dat', 'x', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7', '-mat')

But I want to construct a string array (for everything with a y) in a for loop so that I can change the number of inputs. Does that make sense? So essentially I need to know how to append characters to a string array in a for loop.

Here's the long story, if you want more details:

I want to save a large data set as a matlab file to speed up the load time. I have data series (labeled with letters, ie: a, b, c, etc)) at a variable number of different settings (specified using numbers). For example, in data series a I have a1, a2, a3, a4, a5, a6, a7, etc. I want to automate how I load and save the data so that I can build a data array by simply inputing the series letter and the number of different settings. Right now what I'm doing is this:

save('sigcdata.dat', 'x', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7', '-mat');

Instead I want to use a for loop, something like this:

fnum = input(' enter the series letter: ','s');
fnum2 = input(' enter the number of detunings: ');

for n = 1:fnum2;
    num = num2str(n);
    
    f1 = dlmread(strcat(fnum,num,'oo'),'\t',[0 0 99999 2]); % here I load the data
    f2 = dlmread(strcat(fnum,num,'oc'),'\t',[0 0 99999 2]);
    f3 = dlmread(strcat(fnum,num,'co'),'\t',[0 0 99999 2]);
    f4 = dlmread(strcat(fnum,num,'cc'),'\t',[0 0 99999 2]);
    
    x = (f1(:,1) - f1(1e4,1)); % time
    absa1 = (f1(:,3) - f3(:,3) ) ./ ( f2(:,3) - f4(:,3) ); % do some background stuff
    siga1 = f2(:,2) + f4(:,2) - f1(:,2) - f3(:,2);
    y = siga1 - mean(siga1(1:9e3)); % this is the data I want
    
   eval([genvarname(strcat('y',num)) '= y']); % I assign the data in y a name (y1, y2, y3, etc.) and make it an array.
    
% here's where I create my string array, but I'm not sure how

end

So now I have the everything in a format in which I could use the save function I mentioned earlier. Except that I want to completely automate it. This is where the string array in the for loop scheme comes in. If I could figure out how to append characters to a string array, then I think I could make it work.

For example, if I had a data set with 7 detunings, I would construct a string array: string = y1, y2, y3, y4, y5, y6, y7

but if I only had 3 detunings I would end up with:
string = y1, y2, y3.

And then to save the data I would use:
save('sig',fnum,'data.dat', 'x', string, '-mat')

I'm not sure how much sense this makes or whether this would actually work. But any help would be greatly appreciated! As you can tell, I'm quite the novice here in the matlab world.

Subject: Appending characters to a string array in a for loop?

From: dpb

Date: 22 Feb, 2012 22:43:39

Message: 2 of 4

On 2/22/2012 4:34 PM, M.E.L. wrote:
...

> I want to save a large data set as a matlab file to speed up the load
> time. I have data series (labeled with letters, ie: a, b, c, etc)) at a
> variable number of different settings (specified using numbers). For
> example, in data series a I have a1, a2, a3, a4, a5, a6, a7, etc. I want
> to automate how I load and save the data so that I can build a data
> array by simply inputing the series letter and the number of different
> settings. Right now what I'm doing is this:
>
> save('sigcdata.dat', 'x', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7', '-mat');
>
> Instead I want to use a for loop, something like this:
>
> fnum = input(' enter the series letter: ','s');
> fnum2 = input(' enter the number of detunings: ');
...

> And then to save the data I would use:
> save('sig',fnum,'data.dat', 'x', string, '-mat')
>

I'd rather use the optional wildcard on the variable name in the SAVE
command--like

vname = 'x* y*';
save(fname, vname)

where you can pick up the names in a script, maybe if still think it needed.

Or, return the variables matching a

whos

in the returned name field of the structure and build the list therefrom.

But, to answer the actual question posed...

 >> s=sprintf('y%d ', [1:3])
s =
y1 y2 y3
 >>

--

Subject: Appending characters to a string array in a for loop?

From: M.E.L.

Date: 23 Feb, 2012 00:54:22

Message: 3 of 4

Thanks for your response! Your solution does exactly what I thought I needed ... except that upon trying it I found it didn't work, because what I really need is a string that looks like:

string = 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7'

with the single quotes and commas. Unfortunately, I am again stumped, as I don't know how to put the commas and single quotes in. Seems like it should be trivial, but I can't figure it out!

Thanks!


dpb <none@non.net> wrote in message <ji3r2q$q5o$1@speranza.aioe.org>...
> On 2/22/2012 4:34 PM, M.E.L. wrote:
> ...
>
> > I want to save a large data set as a matlab file to speed up the load
> > time. I have data series (labeled with letters, ie: a, b, c, etc)) at a
> > variable number of different settings (specified using numbers). For
> > example, in data series a I have a1, a2, a3, a4, a5, a6, a7, etc. I want
> > to automate how I load and save the data so that I can build a data
> > array by simply inputing the series letter and the number of different
> > settings. Right now what I'm doing is this:
> >
> > save('sigcdata.dat', 'x', 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7', '-mat');
> >
> > Instead I want to use a for loop, something like this:
> >
> > fnum = input(' enter the series letter: ','s');
> > fnum2 = input(' enter the number of detunings: ');
> ...
>
> > And then to save the data I would use:
> > save('sig',fnum,'data.dat', 'x', string, '-mat')
> >
>
> I'd rather use the optional wildcard on the variable name in the SAVE
> command--like
>
> vname = 'x* y*';
> save(fname, vname)
>
> where you can pick up the names in a script, maybe if still think it needed.
>
> Or, return the variables matching a
>
> whos
>
> in the returned name field of the structure and build the list therefrom.
>
> But, to answer the actual question posed...
>
> >> s=sprintf('y%d ', [1:3])
> s =
> y1 y2 y3
> >>
>
> --

Subject: Appending characters to a string array in a for loop?

From: Steven_Lord

Date: 23 Feb, 2012 03:39:10

Message: 4 of 4



"M.E.L. " <schrodingers.lyon@gmail.com> wrote in message
news:ji42nu$hqe$1@newscl01ah.mathworks.com...
> Thanks for your response! Your solution does exactly what I thought I
> needed ... except that upon trying it I found it didn't work, because what
> I really need is a string that looks like:
>
> string = 'y1', 'y2', 'y3', 'y4', 'y5', 'y6','y7'

Rather than creating variables a1, a2, etc. use the alternate approaches
described in question 6 of the Programming section of the newsgroup FAQ.

http://matlab.wikia.com/wiki/FAQ

> with the single quotes and commas. Unfortunately, I am again stumped, as I
> don't know how to put the commas and single quotes in. Seems like it
> should be trivial, but I can't figure it out!

If you absolutely, positively HAVE to do this, make a cell array of strings:

C = cell(1, 5);
for k = 1:5
    C{k} = sprintf('a%d', k);
end

and use it as a comma-separated list.

save('myfile.mat', C{:});

But again, I STRONGLY recommend you rethink your variable naming system.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us