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:
Help in creating new files

Subject: Help in creating new files

From: Kostas

Date: 9 Dec, 2010 16:48:06

Message: 1 of 7

Hello all, i need your help so as to write my results from a function to a new file (create a new file for any iteration of a for loop). My function is as follow and when i run it i get error in fileno

function toz_lon(new_time,new_gregorian_time,toz2)
toz_usu=squeeze(toz2(13,:,:)); %dimension 144x22979
prevYear = datenum(year(new_time)-1, 12,31);
dayOfYear = new_time-prevYear;
SZA_US=load('SZA_US.dat');
sza=SZA_US(:,3);
for i=1:size(toz_usu,1)
    toz_fin_US=squeeze(toz_usu(i,:));
    fileno=sprintf('toz_fin_US_'+num2str(i)+'.dat'); %create the filename from each iteration of the file
    toz_fin_US=[dayOfYear new_time new_gregorian_time sza toz_fin_US'];
    dlmwrite(fileno,toz_fin_US,'delimiter','\t','precision',6); %write the toz_fin_US matrix to the file
end
end

I would also appreciate any comment for creating a better code, since i am pretty new in Matlab.

Thank you in advance

Subject: Help in creating new files

From: Nic Roberts

Date: 9 Dec, 2010 17:19:05

Message: 2 of 7

"Kostas " <kfragkos@gmail.com> wrote in message <idr186$7v3$1@fred.mathworks.com>...
> Hello all, i need your help so as to write my results from a function to a new file (create a new file for any iteration of a for loop). My function is as follow and when i run it i get error in fileno
>
> function toz_lon(new_time,new_gregorian_time,toz2)
> toz_usu=squeeze(toz2(13,:,:)); %dimension 144x22979
> prevYear = datenum(year(new_time)-1, 12,31);
> dayOfYear = new_time-prevYear;
> SZA_US=load('SZA_US.dat');
> sza=SZA_US(:,3);
> for i=1:size(toz_usu,1)
> toz_fin_US=squeeze(toz_usu(i,:));
> fileno=sprintf('toz_fin_US_'+num2str(i)+'.dat'); %create the filename from each iteration of the file
> toz_fin_US=[dayOfYear new_time new_gregorian_time sza toz_fin_US'];
> dlmwrite(fileno,toz_fin_US,'delimiter','\t','precision',6); %write the toz_fin_US matrix to the file
> end
> end
>
> I would also appreciate any comment for creating a better code, since i am pretty new in Matlab.
>
> Thank you in advance

Hi,

You have to open the file before you can write to it.

fid = fopen(fileno,w);

'fid' will contain the file number. To close the file,

eof = close(fid);

Hope this helps,

Nic

Subject: Help in creating new files

From: Sean de

Date: 9 Dec, 2010 17:26:04

Message: 3 of 7

"Nic Roberts" <dingtheking@googlemail.com> wrote in message
> Hi,
>
> You have to open the file before you can write to it.
>
> fid = fopen(fileno,w);
>
> 'fid' will contain the file number. To close the file,
>
> eof = close(fid);
>
> Hope this helps,
>
> Nic

I thought the same thing at first, but according to:
doc dlmwrite
you don't have to.

Subject: Help in creating new files

From: Nic Roberts

Date: 9 Dec, 2010 17:44:05

Message: 4 of 7

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idr3fc$iub$1@fred.mathworks.com>...
> "Nic Roberts" <dingtheking@googlemail.com> wrote in message
> > Hi,
> >
> > You have to open the file before you can write to it.
> >
> > fid = fopen(fileno,w);
> >
> > 'fid' will contain the file number. To close the file,
> >
> > eof = close(fid);
> >
> > Hope this helps,
> >
> > Nic
>
> I thought the same thing at first, but according to:
> doc dlmwrite
> you don't have to.

Oh yes just tried it, you learn somthing new everyday.

OK my next suggestion is use 'strcat' instead of 'sprint'

Nic

Subject: Help in creating new files

From: ImageAnalyst

Date: 9 Dec, 2010 17:45:27

Message: 5 of 7

On Dec 9, 11:48 am, "Kostas " <kfrag...@gmail.com> wrote:
> Hello all, i need your help so as to write my results from a function to a new file (create a new file for any iteration of a for loop). My function is as follow and when i run it i get error in fileno
-----------------
What is the EXACT error message spit out by MATLAB?

Subject: Help in creating new files

From: ImageAnalyst

Date: 9 Dec, 2010 17:47:12

Message: 6 of 7

You can't do this:
fileno=sprintf('toz_fin_US_'+num2str(i)+'.dat');

You need to do it like norma:

fileno=sprintf('toz_fin_US_%d.dat', i);

Subject: Help in creating new files

From: Kostas

Date: 9 Dec, 2010 18:04:05

Message: 7 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <d7473d60-3e65-45ca-851e-0bd8dd08a7db@j18g2000prn.googlegroups.com>...
> You can't do this:
> fileno=sprintf('toz_fin_US_'+num2str(i)+'.dat');
>
> You need to do it like norma:
>
> fileno=sprintf('toz_fin_US_%d.dat', i);

Thank you all for your responses, but special thanks to ImageAnalyst, it works fine with your suggestion.

The error message i got was

Error in ==> toz_lon at 9
    fileno=sprintf('toz_fin_US_'+num2str(i)+'.dat');

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