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:
"save" within a loop

Subject: "save" within a loop

From: ClauDe De

Date: 11 May, 2010 10:33:04

Message: 1 of 8

Hallo everyone,

ok, I hope I can explain my problem properly...

Within a loop, I would like to save my variables under a special filename which
I want to take out of "B". "B" contains Instrumentnumbers (f.ex.: 2342, 2355, 16019, ... ) Here: size(B) = 1 58

The loop loads a netcdf-file and the variables are created.
    Now I would like to save them in a MAT-file named after the
    Instrumentnumber.
    Something like: save Instr_2342 var1 var2 var3
end of loop

But I got no idea how to let it take the next number out of "B" with every loop and save it under that name.

I have the feeling that this is quite simple, but I got stuck there...

Thanks for your help and comments in advance!!! I really appreciate it!

Subject: "save" within a loop

From: Joshua Arnott

Date: 11 May, 2010 10:49:04

Message: 2 of 8

"ClauDe De" <acerxenite@yahoo.de> wrote in message <hsbbp0$qlt$1@fred.mathworks.com>...
> Hallo everyone,
>
> ok, I hope I can explain my problem properly...
>
> Within a loop, I would like to save my variables under a special filename which
> I want to take out of "B". "B" contains Instrumentnumbers (f.ex.: 2342, 2355, 16019, ... ) Here: size(B) = 1 58
>
> The loop loads a netcdf-file and the variables are created.
> Now I would like to save them in a MAT-file named after the
> Instrumentnumber.
> Something like: save Instr_2342 var1 var2 var3
> end of loop
>
> But I got no idea how to let it take the next number out of "B" with every loop and save it under that name.
>
> I have the feeling that this is quite simple, but I got stuck there...
>
> Thanks for your help and comments in advance!!! I really appreciate it!

Assuming your B variable is a cell array:

for k = 1:length(B)
    % do some processing
    save(sprintf('%s',B{k}),'var1','var2','etc');
end

Subject: "save" within a loop

From: ClauDe

Date: 11 May, 2010 11:38:03

Message: 3 of 8

> Assuming your B variable is a cell array:
>
> for k = 1:length(B)
> % do some processing
> save(sprintf('%s',B{k}),'var1','var2','etc');
> end


Thanks for the quick response!!!!!

Unfortunately, B is no cell-array...

my attempt: turned it into a cell-array (num2cell)
But it's not working either...
Got this error:
??? Error using ==> save
Unable to write file : Invalid argument.

mmh... any clue?!

Thanks!!

Subject: "save" within a loop

From: ClauDe

Date: 11 May, 2010 12:24:04

Message: 4 of 8

> > Assuming your B variable is a cell array:
> >
> > for k = 1:length(B)
> > % do some processing
> > save(sprintf('%s',B{k}),'var1','var2','etc');
> > end

Oh, and when I run this with k = 1
The created MAT-File is called B{k}.mat ...

Subject: "save" within a loop

From: ClauDe

Date: 11 May, 2010 12:38:24

Message: 5 of 8


> Oh, and when I run this with k = 1
> The created MAT-File is called B{k}.mat ...

ups... within my testing"rage", I wrote 'B{k}'... of course the name is B{k} then...
without apostrophe it's still the array- and/or argument error...

Subject: "save" within a loop

From: Joshua Arnott

Date: 11 May, 2010 12:56:05

Message: 6 of 8

"ClauDe " <acerxenite@yahoo.de> wrote in message <hsbfir$pba$1@fred.mathworks.com>...
> > Assuming your B variable is a cell array:
> >
> > for k = 1:length(B)
> > % do some processing
> > save(sprintf('%s',B{k}),'var1','var2','etc');
> > end
>
>
> Thanks for the quick response!!!!!
>
> Unfortunately, B is no cell-array...
>
> my attempt: turned it into a cell-array (num2cell)
> But it's not working either...
> Got this error:
> ??? Error using ==> save
> Unable to write file : Invalid argument.
>
> mmh... any clue?!
>
> Thanks!!

ids = [1:4];
for k = 1:length(ids)
    save(sprintf('instr_%i',ids(k)),'vartosave')
end

Have a look at the sprintf documentation to understand why this works.

help sprintf

Subject: "save" within a loop

From: ImageAnalyst

Date: 11 May, 2010 13:00:25

Message: 7 of 8

ClauDe
If, as you said earlier, B is a numerical array, not a cell array,
then do this:

folder = 'c:\whatever';
baseFileName = sprintf('Instr_%d', B(k));
fullFileName = fullfile(folder, baseFileName);
save(fullFileName, 'var1', 'var2');

The key is to use parentheses instead of braces.

Subject: "save" within a loop

From: ClauDe

Date: 11 May, 2010 13:14:06

Message: 8 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <24cae0c7-d714-4ee1-a1cd-1d992c251b57@24g2000yqy.googlegroups.com>...
> ClauDe
> If, as you said earlier, B is a numerical array, not a cell array,
> then do this:
>
> folder = 'c:\whatever';
> baseFileName = sprintf('Instr_%d', B(k));
> fullFileName = fullfile(folder, baseFileName);
> save(fullFileName, 'var1', 'var2');
>
> The key is to use parentheses instead of braces.

It works!!!!!!! :-)
THANK YOU SO MUCH!!!
 

Tags for 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