Thread Subject: write string to file

Subject: write string to file

From: Jos

Date: 12 Nov, 2009 11:58:02

Message: 1 of 11

I want to read a series of strings under each other in a txt file.
I tried doing this:
fid=fopen(save_name, 'w');

for i=1:length(model_specifications)
        cell=cell2mat(model_specifications(1,i))
        fprintf(fid,'\n',cell);

fclose(fid)

The entries in model_specifications are all strings.

But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other. (I tried to copy-paste them here, but that didn't work)

Does anyone know what I'm doing wrong?

Subject: write string to file

From: Rune Allnor

Date: 12 Nov, 2009 12:37:24

Message: 2 of 11

On 12 Nov, 12:58, "Jos " <jappedane...@mathworks.com> wrote:
> I want to read a series of strings under each other in a txt file.
> I tried doing this:
> fid=fopen(save_name, 'w');
>
> for i=1:length(model_specifications)
>         cell=cell2mat(model_specifications(1,i))
>         fprintf(fid,'\n',cell);
>
> fclose(fid)
>
> The entries in model_specifications are all strings.
>
> But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other.  (I tried to copy-paste them here, but that didn't work)
>
> Does anyone know what I'm doing wrong?

I don't *know*, but I *suspect* that you dump raw binary
data to the file instead of the numbers you want. You would
certainly need to look at the formatting characters in the
documentation on FPRINTF (the details might be found with one
of its cousinse, like FSCANF, SPRINTF or SSCANF), and possibly
also how to access elements in a cell array.

Rune

Subject: write string to file

From: Jos

Date: 12 Nov, 2009 12:59:04

Message: 3 of 11

But I don't want to write numbers to the file, I want to write text (either it being strings or characters).
cell=cell2mat(model_specifications(1,i)) gives me the desired lines of text, but for some reason I can not write them to the file.

Rune Allnor <allnor@tele.ntnu.no> wrote in message <ec5e4d81-a97e-4e35-a5e8-756d64b7fb3e@h34g2000yqm.googlegroups.com>...
> On 12 Nov, 12:58, "Jos " <jappedane...@mathworks.com> wrote:
> > I want to read a series of strings under each other in a txt file.
> > I tried doing this:
> > fid=fopen(save_name, 'w');
> >
> > for i=1:length(model_specifications)
> > ? ? ? ? cell=cell2mat(model_specifications(1,i))
> > ? ? ? ? fprintf(fid,'\n',cell);
> >
> > fclose(fid)
> >
> > The entries in model_specifications are all strings.
> >
> > But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other. ?(I tried to copy-paste them here, but that didn't work)
> >
> > Does anyone know what I'm doing wrong?
>
> I don't *know*, but I *suspect* that you dump raw binary
> data to the file instead of the numbers you want. You would
> certainly need to look at the formatting characters in the
> documentation on FPRINTF (the details might be found with one
> of its cousinse, like FSCANF, SPRINTF or SSCANF), and possibly
> also how to access elements in a cell array.
>
> Rune

Subject: write string to file

From: Jos

Date: 12 Nov, 2009 13:06:01

Message: 4 of 11

I don't want to put numbers in the textfile, I only want to put text in there. Thats why I put read each line (from the cell array) using this:
cell=cell2mat(model_specifications(1,i))
And it does indeed give me the desired line (and I think is is a string, since ' ' is placed around it. Is fprintf able to write strings (or characters) to an output file? Or is it just numbers?


Rune Allnor <allnor@tele.ntnu.no> wrote in message <ec5e4d81-a97e-4e35-a5e8-756d64b7fb3e@h34g2000yqm.googlegroups.com>...
> On 12 Nov, 12:58, "Jos " <jappedane...@mathworks.com> wrote:
> > I want to read a series of strings under each other in a txt file.
> > I tried doing this:
> > fid=fopen(save_name, 'w');
> >
> > for i=1:length(model_specifications)
> > ? ? ? ? cell=cell2mat(model_specifications(1,i))
> > ? ? ? ? fprintf(fid,'\n',cell);
> >
> > fclose(fid)
> >
> > The entries in model_specifications are all strings.
> >
> > But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other. ?(I tried to copy-paste them here, but that didn't work)
> >
> > Does anyone know what I'm doing wrong?
>
> I don't *know*, but I *suspect* that you dump raw binary
> data to the file instead of the numbers you want. You would
> certainly need to look at the formatting characters in the
> documentation on FPRINTF (the details might be found with one
> of its cousinse, like FSCANF, SPRINTF or SSCANF), and possibly
> also how to access elements in a cell array.
>
> Rune

Subject: write string to file

From: Shanmugam Kannappan

Date: 12 Nov, 2009 13:25:04

Message: 5 of 11

"Jos " <jappedaneels@mathworks.com> wrote in message <hdh17p$o29$1@fred.mathworks.com>...
> I don't want to put numbers in the textfile, I only want to put text in there. Thats why I put read each line (from the cell array) using this:
> cell=cell2mat(model_specifications(1,i))
> And it does indeed give me the desired line (and I think is is a string, since ' ' is placed around it. Is fprintf able to write strings (or characters) to an output file? Or is it just numbers?
>
>
> Rune Allnor <allnor@tele.ntnu.no> wrote in message <ec5e4d81-a97e-4e35-a5e8-756d64b7fb3e@h34g2000yqm.googlegroups.com>...
> > On 12 Nov, 12:58, "Jos " <jappedane...@mathworks.com> wrote:
> > > I want to read a series of strings under each other in a txt file.
> > > I tried doing this:
> > > fid=fopen(save_name, 'w');
> > >
> > > for i=1:length(model_specifications)
> > > ? ? ? ? cell=cell2mat(model_specifications(1,i))
> > > ? ? ? ? fprintf(fid,'\n',cell);
> > >
> > > fclose(fid)
> > >
> > > The entries in model_specifications are all strings.
> > >
> > > But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other. ?(I tried to copy-paste them here, but that didn't work)
> > >
> > > Does anyone know what I'm doing wrong?
> >
> > I don't *know*, but I *suspect* that you dump raw binary
> > data to the file instead of the numbers you want. You would
> > certainly need to look at the formatting characters in the
> > documentation on FPRINTF (the details might be found with one
> > of its cousinse, like FSCANF, SPRINTF or SSCANF), and possibly
> > also how to access elements in a cell array.
> >
> > Rune

Hi Jos,

Can you try this way,

fid=fopen(save_name, 'w');
 for i=1:length(model_specifications)
 cell=cell2mat(model_specifications(1,i))
 fprintf(fid,[cell '\n']);
 fclose(fid).
Here I assumed that cell is a string.
Open it in MATLAB Editor itself & Check.
Sometimes I saw like in notepad you will have a box kind of thing in place of newline character.
Open it in matlab edito & see If it works.

Shan...

Subject: write string to file

From: Jos

Date: 12 Nov, 2009 13:44:04

Message: 6 of 11

This seems to work, thx! Only problem is that this prints everything on one line. After each string, it puts again a square. I guess this is where the enters (new line) are supposed to be. Is there any way to overcome this (so really place the enters instead of those stupid squares :p)?

"Shanmugam Kannappan" <shanmugambe@gmail.com> wrote in message <hdh2bg$3tu$1@fred.mathworks.com>...
> "Jos " <jappedaneels@mathworks.com> wrote in message <hdh17p$o29$1@fred.mathworks.com>...
> > I don't want to put numbers in the textfile, I only want to put text in there. Thats why I put read each line (from the cell array) using this:
> > cell=cell2mat(model_specifications(1,i))
> > And it does indeed give me the desired line (and I think is is a string, since ' ' is placed around it. Is fprintf able to write strings (or characters) to an output file? Or is it just numbers?
> >
> >
> > Rune Allnor <allnor@tele.ntnu.no> wrote in message <ec5e4d81-a97e-4e35-a5e8-756d64b7fb3e@h34g2000yqm.googlegroups.com>...
> > > On 12 Nov, 12:58, "Jos " <jappedane...@mathworks.com> wrote:
> > > > I want to read a series of strings under each other in a txt file.
> > > > I tried doing this:
> > > > fid=fopen(save_name, 'w');
> > > >
> > > > for i=1:length(model_specifications)
> > > > ? ? ? ? cell=cell2mat(model_specifications(1,i))
> > > > ? ? ? ? fprintf(fid,'\n',cell);
> > > >
> > > > fclose(fid)
> > > >
> > > > The entries in model_specifications are all strings.
> > > >
> > > > But when I look at the output txt-file, all I see is 17 squares (17 is the lenght of model_specifications), that aren't even under each other. ?(I tried to copy-paste them here, but that didn't work)
> > > >
> > > > Does anyone know what I'm doing wrong?
> > >
> > > I don't *know*, but I *suspect* that you dump raw binary
> > > data to the file instead of the numbers you want. You would
> > > certainly need to look at the formatting characters in the
> > > documentation on FPRINTF (the details might be found with one
> > > of its cousinse, like FSCANF, SPRINTF or SSCANF), and possibly
> > > also how to access elements in a cell array.
> > >
> > > Rune
>
> Hi Jos,
>
> Can you try this way,
>
> fid=fopen(save_name, 'w');
> for i=1:length(model_specifications)
> cell=cell2mat(model_specifications(1,i))
> fprintf(fid,[cell '\n']);
> fclose(fid).
> Here I assumed that cell is a string.
> Open it in MATLAB Editor itself & Check.
> Sometimes I saw like in notepad you will have a box kind of thing in place of newline character.
> Open it in matlab edito & see If it works.
>
> Shan...

Subject: write string to file

From: Leslie McBrayer

Date: 12 Nov, 2009 13:45:20

Message: 7 of 11

>I want to read a series of strings under each other in a txt file.
> I tried doing this:
> fid=fopen(save_name, 'w');
>
> for i=1:length(model_specifications)
> cell=cell2mat(model_specifications(1,i))
> fprintf(fid,'\n',cell);
>
> fclose(fid)
>
> The entries in model_specifications are all strings.
>
> But when I look at the output txt-file, all I see is 17 squares (17 is the
> lenght of model_specifications), that aren't even under each other. (I
> tried to copy-paste them here, but that didn't work)
>
> Does anyone know what I'm doing wrong?

When you call fprintf, you need to tell it that you are printing strings.
Your current call only tells fprintf to print newline characters (\n).

Try something like this:

fprintf(fid, '%s\n', cell)

For more info, see this section of the User Guide:

http://www.mathworks.com/access/helpdesk/help/techdoc/import_export/f5-15544.html#br2ypq2-1

Subject: write string to file

From: Leslie McBrayer

Date: 12 Nov, 2009 13:47:23

Message: 8 of 11


"Jos " <jappedaneels@mathworks.com> wrote in message
news:hdh3f4$d99$1@fred.mathworks.com...
> This seems to work, thx! Only problem is that this prints everything on
> one line. After each string, it puts again a square. I guess this is where
> the enters (new line) are supposed to be. Is there any way to overcome
> this (so really place the enters instead of those stupid squares :p)?

If you're using Notepad, you need to use '\r\n' instead of '\n'.

Subject: write string to file

From: Jos

Date: 12 Nov, 2009 14:13:01

Message: 9 of 11

"Leslie McBrayer" <lmcbrayer@mathworks.com> wrote in message <hdh3ld$pjn$1@fred.mathworks.com>...
>
> "Jos " <jappedaneels@mathworks.com> wrote in message
> news:hdh3f4$d99$1@fred.mathworks.com...
> > This seems to work, thx! Only problem is that this prints everything on
> > one line. After each string, it puts again a square. I guess this is where
> > the enters (new line) are supposed to be. Is there any way to overcome
> > this (so really place the enters instead of those stupid squares :p)?
>
> If you're using Notepad, you need to use '\r\n' instead of '\n'.
>

Thanks, this works, GREAT :)

Subject: write string to file

From: Steven Lord

Date: 12 Nov, 2009 14:49:59

Message: 10 of 11


"Leslie McBrayer" <lmcbrayer@mathworks.com> wrote in message
news:hdh3ld$pjn$1@fred.mathworks.com...
>
> "Jos " <jappedaneels@mathworks.com> wrote in message
> news:hdh3f4$d99$1@fred.mathworks.com...
>> This seems to work, thx! Only problem is that this prints everything on
>> one line. After each string, it puts again a square. I guess this is
>> where the enters (new line) are supposed to be. Is there any way to
>> overcome this (so really place the enters instead of those stupid squares
>> :p)?
>
> If you're using Notepad, you need to use '\r\n' instead of '\n'.

Rather than modifying the FPRINTF call this way, I recommend modifying the
FOPEN call at the beginning of the code. Copying it in from the original
post in this thread:

> fid=fopen(save_name, 'w');
> for i=1:length(model_specifications)
> cell=cell2mat(model_specifications(1,i))
> fprintf(fid,'\n',cell);
>
> fclose(fid)

You said in that original posting that you're opening a text file -- so why
aren't you opening it in text mode?

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html

Review the last paragraph or so in the description of the "permission" input
argument.

There are a couple more issues I noticed with your code:

The way you've written your FPRINTF call, the _only_ thing that should be
printed to the file at each iteration should be the newline, not the string
stored in the variable cell (which, BTW, I would rename, as CELL is the name
of a function commonly used to create a cell array of a certain size.

Be careful about using the LENGTH function with something that could be a
matrix. If you're looking to process each of the elements of a variable
that could be a vector, matrix, or array I would use NUMEL. If you
specifically want to process each column, as it appears is the case here,
use SIZE with a dimension input. Compare LENGTH, NUMEL, and SIZE(x, 2) for
tall thin x inputs [x = ones(100, 3)] and short wide x inputs [x = ones(3,
100)].

Rather than calling CELL2MAT on a scalar cell array to extract the (I
assume) char array stored inside, use curly brace indexing.

Finally, I think you missed an END when you copied and pasted your code.
You really want that FCLOSE outside the FOR loop, I'm guessing.


fid=fopen(save_name, 'wt');
for i=1:size(model_specifications, 2)
    MS = model_specifications{1,i};
    fprintf(fid, '%s\n', MS);
end
fclose(fid)


--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: write string to file

From: dpb

Date: 12 Nov, 2009 15:11:24

Message: 11 of 11

Steven Lord wrote:
...
> Rather than modifying the FPRINTF call this way, I recommend modifying the
> FOPEN call at the beginning of the code. Copying it in from the original
> post in this thread:
>
>> fid=fopen(save_name, 'w');
...
>
> You said in that original posting that you're opening a text file -- so why
> aren't you opening it in text mode?
...

And just to re-emphasize the point (as I initially misread the
intent(+)), Steven's point is the above fopen() call, lacking the
trailing 't' for "text mode" is by default a 'binary' file format per
fopen() documentation.

The correct form for writing text follows...

> fid=fopen(save_name, 'wt');

...

(+) First time I read the posting, I thought the "copying from original
post" was intended to imply that was what had been recommended. It
wasn't until I parsed the whole message it became clear that Steven was
(as per usual :) ) was correct; only that it was, I thought, perhaps
possible to misread intent here unless read carefully through to the end.

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com