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:
eval save text files without putting extension filename

Subject: eval save text files without putting extension filename

From: Kuo-Hsien

Date: 16 Oct, 2011 19:02:28

Message: 1 of 5

Hi all,

Here is my statement:

for ii = year_1st:year_end
    file_name = num2str(ii - year_1st +1)
    ...
    ...
    eval(['save ', file_name,'-ascii'])
end


However, the output file names are always with "-ascii.mat."

Could you please let me know how to save my files without any extension filename?

How to put the file_name in the header of each file?

Thanks a lot.

Subject: eval save text files without putting extension filename

From: TideMan

Date: 16 Oct, 2011 19:12:53

Message: 2 of 5

On Oct 17, 8:02 am, "Kuo-Hsien" <mchan...@hotmail.com> wrote:
> Hi all,
>
> Here is my statement:
>
> for ii = year_1st:year_end
>     file_name = num2str(ii - year_1st +1)
>     ...
>     ...
>     eval(['save ', file_name,'-ascii'])
> end
>
> However, the output file names are always with "-ascii.mat."
>
> Could you please let me know how to save my files without any extension filename?
>
> How to put the file_name in the header of each file?
>
> Thanks a lot.

Use the functional form of save:
save(file_name,'-ascii')

Subject: eval save text files without putting extension filename

From: ImageAnalyst

Date: 16 Oct, 2011 19:15:48

Message: 3 of 5

And *I* use sprintf() to make up the filename instead of the way
you're doing it. Be sure to include the folder and extension for
robustness. You might try to become familiar with fullfile() and
fileparts() also - you'll find them useful often.

Subject: eval save text files without putting extension filename

From: Kuo-Hsien

Date: 17 Oct, 2011 02:50:14

Message: 4 of 5

Yes. Thanks for your reply. That helps.
Could you please explain more about how to generate filename automatically using eval?!
How to replace "DNX" in my case by "file_name" (i.e.., 1, 2, 3, 4, 5, ...)?

Here is my statement again:

for ii = year_1st:year_end
    file_name = num2str(ii - year_1st +1)
    fid = find(yy == ii);
    DNX = [b1(fid), b2(fid), b3(fid),b4(fid)]';
    id = fopen('/output/file_name', 'w');
    fprintf(id, ' %g\t%7.3f\t%7.3f\t%7.3f\n', DNX);
    fclose(id);
end

Subject: eval save text files without putting extension filename

From: Steven_Lord

Date: 17 Oct, 2011 03:30:41

Message: 5 of 5



"Kuo-Hsien" <mchangks@hotmail.com> wrote in message
news:j7g556$it5$1@newscl01ah.mathworks.com...
> Yes. Thanks for your reply. That helps. Could you please explain more
> about how to generate filename automatically using eval?!

Do NOT use EVAL.

> How to replace "DNX" in my case by "file_name" (i.e.., 1, 2, 3, 4, 5,
> ...)?
>
> Here is my statement again:
>
> for ii = year_1st:year_end
> file_name = num2str(ii - year_1st +1)
> fid = find(yy == ii);
> DNX = [b1(fid), b2(fid), b3(fid),b4(fid)]';
> id = fopen('/output/file_name', 'w');

For such a simple example you could just use concatenation.

id = fopen(['/output/' file_name], 'w');

If it's more complicated, look at FULLFILE.

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

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