Thread Subject:
Writing Matrices to text file and printing additional text in specific format

Subject: Writing Matrices to text file and printing additional text in specific format

From: Eli

Date: 20 Jun, 2012 17:53:06

Message: 1 of 17

Hi, I am trying to write data from 4 matrices to a text file. The data needs to be written in a specific format. I am having some trouble with one part of this. The details are below and my attempts at the solution are also included.

% nmat is the number of matrices to be written to a text file
nmat = 4;

I have then created 4 matrices:

MMM = rand(5,6);
NNN = rand(3,6);
PPP = rand(6,6);
QQQ = rand(4,6);

These now must be written to a text file, in the following format:

A. I need to offset all 4 matrices from the left side of the destination file (myfile.txt) to where matrix data is to be written.
I have done (example for matrix MMM) this using:
dlmwrite('myfile.txt', MMM, coffset, 1, 'delimiter', '\t', 'precision', '%.6f')

B. After printing the first matrix MMM, I needed to append each additional matrix (NNN, PPP, QQQ) to the file, in such a way that they are offset one row below the previous matrix and one column from the left side of the destination file (myfile.txt) to where matrix data is to be written.
I have done this using:
dlmwrite('myfile.txt', NNN, coffset, 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
dlmwrite('myfile.txt', PPP, coffset, 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
dlmwrite('myfile.txt', QQQ, coffset, 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')

This appears to be working as I require. However, I now have one more important step (with 2 parts) that needs to be done. I am having difficulty with this.

C. I now need to print the following information in the file myfile.txt:
1. Insert or print 2 blank rows or lines above matrix MMM. After doing this, the first entry in the matrix MMM will be in Row 3 Column 2 (or Line 3 Column 2).
a. In Row 1 Column 2, I need to insert or print the number 2.
b. In Row 2 Column 2, I need to insert or print the number of columns in matrix MMM.
c. In Row 2 Column 3, I need to insert or print the number of rows in matrix MMM.
d. In Row 2 Column 4, I need to insert or print the value of the variable nmat (which is 4).

2. The last row of matrix QQQ is on row n (or line n). Here, n = 23 (2 + 5 + 1 + 3 + 1 + 6 + 1 + 4). After this row, I need to print the following information:
a. In Row n + 1 (24) Column 2, I need to insert or print the name of the first matrix.i.e. MMM. If this cannot be done, I would like to print AAA.
b. In Row n + 2 (25) Column 2, I need to insert or print the name of the second matrix.i.e. NNN. If this cannot be done, I would like to print BBB.
c. In Row n + 3 (26) Column 1, I need to insert or print the name of the third matrix.i.e. PPP. If this cannot be done, I would like to print CCC.
d. In Row n + 4 (27) Column 1, I need to insert or print the name of the fourth matrix.i.e. QQQ. If this cannot be done, I would like to print ZZZ.

If you need some more information, please let me know.

-------------------------------X-------------------------------
My attempts at the solution:
For C 1.), could I just use this?:
dlmwrite('myfile.txt', MMM, roffset, 3, coffset, 1, 'delimiter', '\t', 'precision', '%.6f')

For C 1. b/c, I can get the size of the matrix MMM:
[rows,cols] = size(MMM);

But I am not sure how to print this information (rows and cols) to the text file, in a particular location.
-------------------------------X-------------------------------

Could you please help me out with step C?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 20 Jun, 2012 18:25:01

Message: 2 of 17

On 6/20/2012 12:53 PM, Eli wrote:
> Hi, I am trying to write data from 4 matrices to a text file. The data
> needs to be written in a specific format. I am having some trouble with
> one part of this. The details are below and my attempts at the solution
> are also included.
>
> % nmat is the number of matrices to be written to a text file
> nmat = 4;
>
> I have then created 4 matrices:
>
> MMM = rand(5,6);
> NNN = rand(3,6);
> PPP = rand(6,6);
> QQQ = rand(4,6);
>
> These now must be written to a text file, in the following format:
>
...

> This appears to be working as I require. However, I now have one more
> important step (with 2 parts) that needs to be done. I am having
> difficulty with this.
>
> C. I now need to print the following information in the file myfile.txt:
> 1. Insert or print 2 blank rows or lines above matrix MMM. After doing
> this, the first entry in the matrix MMM will be in Row 3 Column 2 (or
> Line 3 Column 2).
> a. In Row 1 Column 2, I need to insert or print the number 2.
> b. In Row 2 Column 2, I need to insert or print the number of columns in
> matrix MMM.
> c. In Row 2 Column 3, I need to insert or print the number of rows in
> matrix MMM.
> d. In Row 2 Column 4, I need to insert or print the value of the
> variable nmat (which is 4).

Well, the thing about sequential files is that they are...well,
"sequential". If you want a direct-access, record-accessible file, then
use a format that supports it. Flat text files don't.

So, the solution to the problem is to write the first two lines first,
then append after that w/ the r,c option in dlmwrite().

>
> 2. The last row of matrix QQQ is on row n (or line n). Here, n = 23 (2 +
> 5 + 1 + 3 + 1 + 6 + 1 + 4). After this row, I need to print the
> following information:
> a. In Row n + 1 (24) Column 2, I need to insert or print the name of the
> first matrix.i.e. MMM. If this cannot be done, I would like to print AAA.
> b. In Row n + 2 (25) Column 2, I need to insert or print the name of the
> second matrix.i.e. NNN. If this cannot be done, I would like to print BBB.
> c. In Row n + 3 (26) Column 1, I need to insert or print the name of the
> third matrix.i.e. PPP. If this cannot be done, I would like to print CCC.
> d. In Row n + 4 (27) Column 1, I need to insert or print the name of the
> fourth matrix.i.e. QQQ. If this cannot be done, I would like to print ZZZ.

Now you're stuck...dlmwrite() doesn't handle cell data and treats
character data as individual characters, not as delimited strings.

AFAIK, your only choice here will be to reopen the file with fopen() w/
the 'a' append permission attribute and then use fprintf() to write the
character data.

Sotoo,

fid=fopen('myfile.txt','at+');
% test for valid fid here, of course...
fprintf(fid,'\tMMM\n');
fprintf(fid,'\tNNN\n');
fprintf(fid,'PPP\n');
fprintf(fid,'OOO\n');
fid=fclose(fid);

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 20 Jun, 2012 19:17:15

Message: 3 of 17

dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
> Now you're stuck...dlmwrite() doesn't handle cell data and treats
> character data as individual characters, not as delimited strings.
>
> AFAIK, your only choice here will be to reopen the file with fopen() w/
> the 'a' append permission attribute and then use fprintf() to write the
> character data.
>
> Sotoo,
>
> fid=fopen('myfile.txt','at+');
> % test for valid fid here, of course...
> fprintf(fid,'\tMMM\n');
> fprintf(fid,'\tNNN\n');
> fprintf(fid,'PPP\n');
> fprintf(fid,'OOO\n');
> fid=fclose(fid);

-----------X------------

Okay, let me start with this part.
1. I get what you're doing with the fprintf commands. But I am confused by "test for valid fid here, of course...". I might be missing something simple. Could you explain what you mean by this?

2. Is there a way to do this with some structure/command that can handle cell data, as you suggested earlier?

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 20 Jun, 2012 19:25:07

Message: 4 of 17

dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
...
> fid=fopen('myfile.txt','at+');
> % test for valid fid here, of course...
> fprintf(fid,'\tMMM\n');
> fprintf(fid,'\tNNN\n');
> fprintf(fid,'PPP\n');
> fprintf(fid,'OOO\n');
> fid=fclose(fid);
>
> --

Regarding the last 2 fprintf commands, I'm presuming that the missing \t avoids moving into column 2?i.e. it stays in column 1, as required....is this correct?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 20 Jun, 2012 19:59:24

Message: 5 of 17

On 6/20/2012 2:17 PM, Eli wrote:
> dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
>> Now you're stuck...dlmwrite() doesn't handle cell data and treats
>> character data as individual characters, not as delimited strings.
>>
>> AFAIK, your only choice here will be to reopen the file with fopen()
>> w/ the 'a' append permission attribute and then use fprintf() to write
>> the character data.
>>
>> Sotoo,
>>
>> fid=fopen('myfile.txt','at+');
>> % test for valid fid here, of course...
>> fprintf(fid,'\tMMM\n');
>> fprintf(fid,'\tNNN\n');
>> fprintf(fid,'PPP\n');
>> fprintf(fid,'OOO\n');
>> fid=fclose(fid);
>
> -----------X------------
>
> Okay, let me start with this part.
> 1. I get what you're doing with the fprintf commands. But I am confused
> by "test for valid fid here, of course...". I might be missing something
> simple. Could you explain what you mean by this?

Sure, fopen() returns fid>0 for success, -1 for failure to successfully
open the file. Defensive programming and all that... :)


> 2. Is there a way to do this with some structure/command that can handle
> cell data, as you suggested earlier?

In my release of Matlab, no. I looked at current release online doc for
dlmwrite() and the list of file i/o functions and didn't see anything
that looks like it has changed in that regard. You'll have to search
your version's help to see if I missed something useful as it certainly
couldn't be said to have been an exhaustive search but it surely didn't
jump out at me, no.

--

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 20 Jun, 2012 20:08:31

Message: 6 of 17

On 6/20/2012 2:25 PM, Eli wrote:
> dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
> ....
>> fid=fopen('myfile.txt','at+');
>> % test for valid fid here, of course...
>> fprintf(fid,'\tMMM\n');
>> fprintf(fid,'\tNNN\n');
>> fprintf(fid,'PPP\n');
>> fprintf(fid,'OOO\n');
>> fid=fclose(fid);
>>
>> --
>
> Regarding the last 2 fprintf commands, I'm presuming that the missing \t
> avoids moving into column 2?i.e. it stays in column 1, as required....is
> this correct?

Correct. One column leading delimiter in the first case, none in the
second. Should do what you asked iiuc the desire.

OBTW, you could do something w/ cellstrings w/ fprintf if it's
convenient to build some of the data that way...here's an example using
another sample posted for another question that just happened to be in
my workspace still...

 > c='N0';for ix=1:5,c=strvcat(c,['N' num2str(ix)]);end
 >> cs=cellstr(c)
cs =
     'N0'
     'N1'
     'N2'
     'N3'
     'N4'
     'N5'
 >> fprintf('%s\n',cs{:})
N0
N1
N2
N3
N4
N5
 >> fprintf('%s\n',cs)
??? Error using ==> fprintf
Function 'fprintf' not defined for variables of class 'cell'.

 >>

Note the key is to dereference the cell array w/ the curly brackets '{}'

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 21 Jun, 2012 15:24:07

Message: 7 of 17

dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
> Well, the thing about sequential files is that they are...well,
> "sequential". If you want a direct-access, record-accessible file, then
> use a format that supports it. Flat text files don't.
>
> So, the solution to the problem is to write the first two lines first,
> then append after that w/ the r,c option in dlmwrite().

OKAY, regarding the first 2 lines, here is my attempt and my follow-up questions:

Get the size of matrix MMM:
[a,b] = size(MMM);

Open the file myfile.txt:
fid=fopen('myfile.txt','at+');

Print the number 2 in row 1 column 2:
fprintf(fid'\t'2'\n');

Print rows, columns and number of matrices (in columns 2, 3 and 4 respectively):
fprintf(fid, %f %f %f\n,'\ta b nmat\n');

Q1. I'm not too certain about how to incorporate the spacing here. The 3 numbers must be separated by the same spacing as the matrix elements.i.e. they must be in the same columns (columns 2, 3 and 4) and aligned in the same way (left, right or justified). Is fprintf the way to do this, or am I using it wrongly?

Close the file myfile.txt:
fid=fclose(fid);

Q2. If I am going to continue writing with dlmwrite, do I need to close the file after printing the first 2 lines (i.e. do I need this command)?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 21 Jun, 2012 18:03:32

Message: 8 of 17

On 6/21/2012 10:24 AM, Eli wrote:
> dpb <none@non.net> wrote in message <jrt4hv$da6$1@speranza.aioe.org>...
...

>> So, the solution to the problem is to write the first two lines first,
>> then append after that w/ the r,c option in dlmwrite().
>
> OKAY, regarding the first 2 lines, here is my attempt and my follow-up
> questions:
>
> Get the size of matrix MMM:
> [a,b] = size(MMM);
>
> Open the file myfile.txt:
> fid=fopen('myfile.txt','at+');
>
> Print the number 2 in row 1 column 2:
> fprintf(fid'\t'2'\n');

If you want to be sure you're starting a new file (and if you're writing
the first two lines, that's really your only choice w/ sequential file
w/o a bunch more effort), here you don't want the 'a' but to write and
create a new file if file of the name doesn't exist or truncate the
contents and start over if it does. That's the 'w' permission.

> Print rows, columns and number of matrices (in columns 2, 3 and 4
> respectively):
> fprintf(fid, %f %f %f\n,'\ta b nmat\n');
>
> Q1. I'm not too certain about how to incorporate the spacing here. The 3
> numbers must be separated by the same spacing as the matrix
> elements.i.e. they must be in the same columns (columns 2, 3 and 4) and
> aligned in the same way (left, right or justified). Is fprintf the way
> to do this, or am I using it wrongly?

Well, now we've come to a case of you're asking for a lot more
formatting than previously. First we need to settle on what you mean by
a column here--since you had been using dlmwrite() which writes
delimited fields, I had presumed you were using that definition as well.

If you're now talking about actual columns as in columns in a
fixed-length record, that's something else again.

And, by default dlmwrite uses 'free format' to write the data elements
as far as what particular format string the value will use as will/does
a plain '%f' in a format string.

If you need more control than that you'll have to write specific format
strings including field widths, precision, and justification (altho
iirc, C/Matlab justifies fields to the right unless specifically told
different in the format string).

So, the answer specifically is "it depends" and you haven't specified
the requirements sufficiently to write a specific format.

The above mixing \t in the format and in the output list probably isn't
going to work altho I didn't try it specifically...

NB you can try all this stuff out interactively and see what you get to
work through the details...

> Close the file myfile.txt:
> fid=fclose(fid);
>
> Q2. If I am going to continue writing with dlmwrite, do I need to close
> the file after printing the first 2 lines (i.e. do I need this command)?

Well, yes, you don't want to be holding it open w/ one file handle and
then start mucking on it w/ another call...

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 21 Jun, 2012 21:29:05

Message: 9 of 17

dpb <none@non.net> wrote in message <jrvnlm$jtj$1@speranza.aioe.org>...
>
> Well, now we've come to a case of you're asking for a lot more
> formatting than previously. First we need to settle on what you mean by
> a column here--since you had been using dlmwrite() which writes
> delimited fields, I had presumed you were using that definition as well.
>
> If you're now talking about actual columns as in columns in a
> fixed-length record, that's something else again.
>
> And, by default dlmwrite uses 'free format' to write the data elements
> as far as what particular format string the value will use as will/does
> a plain '%f' in a format string.
>
> If you need more control than that you'll have to write specific format
> strings including field widths, precision, and justification (altho
> iirc, C/Matlab justifies fields to the right unless specifically told
> different in the format string).
>
> So, the answer specifically is "it depends" and you haven't specified
> the requirements sufficiently to write a specific format.
>
> The above mixing \t in the format and in the output list probably isn't
> going to work altho I didn't try it specifically...
>
> NB you can try all this stuff out interactively and see what you get to
> work through the details...
>

Sorry for the confusion:
"since you had been using dlmwrite() which writes
> delimited fields, I had presumed you were using that definition as well."

Yes, that is what I meant.



> > Close the file myfile.txt:
> > fid=fclose(fid);
> >
> > Q2. If I am going to continue writing with dlmwrite, do I need to close
> > the file after printing the first 2 lines (i.e. do I need this command)?
>
> Well, yes, you don't want to be holding it open w/ one file handle and
> then start mucking on it w/ another call...
>
> --

Yeah, I would need to close it then. It seemed counter intuitive to me to open a file, write 2 lines, close it. Then dlmwrite() to the same file. That's what was throwing me off.

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 21 Jun, 2012 23:35:09

Message: 10 of 17

On 6/21/2012 4:29 PM, Eli wrote:
...

> Yeah, I would need to close it then. It seemed counter intuitive to me
> to open a file, write 2 lines, close it. Then dlmwrite() to the same
> file. That's what was throwing me off.

It would be if you were doing all the writing w/ low-level io functions
using the same file handle. But, dlmwrite is a high-level routine that
is given a file name so it does the opening and closing behind the
scenes with its own local fhandle.

--

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 21 Jun, 2012 23:49:39

Message: 11 of 17

On 6/21/2012 6:35 PM, dpb wrote:
> On 6/21/2012 4:29 PM, Eli wrote:
> ....
>
>> Yeah, I would need to close it then. It seemed counter intuitive to me
>> to open a file, write 2 lines, close it. Then dlmwrite() to the same
>> file. That's what was throwing me off.
>
> It would be if you were doing all the writing w/ low-level io functions
> using the same file handle. But, dlmwrite is a high-level routine that
> is given a file name so it does the opening and closing behind the
> scenes with its own local fhandle.

And, just to point out the obvious, when you make multiple calls to
dlmwrite() you're doing the same thing, too...

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 27 Jun, 2012 17:37:08

Message: 12 of 17

Here's an updated version of my code with the all the above commands:

[/code]
x_coord = 61;
y_coord = 3;
nmat=5;
 
MMM = zeros(y_coord,x_coord);
NNN = zeros(y_coord,x_coord);
PPP = zeros(y_coord,x_coord);
QQQ = zeros(y_coord,x_coord);
RRR = zeros(y_coord,x_coord);
[a,b]=size(MMM);
 
fid1=fopen('myfile.txt','at+');
fprintf(fid1,'\t2\n');
fprintf(fid1,'\t %f %f %f\n', a, b, nmat);
fclose(fid1);
 
MMM = rand(x_coord,y-coord);
NNN = rand(x_coord,y-coord);
PPP = rand(x_coord,y-coord);
QQQ = rand(x_coord,y-coord);
RRR = rand(x_coord,y-coord);
dlmwrite('myfile.txt',MMM, 'coffset', 1, 'roffset', 2, 'delimiter', '\t', 'precision', '%.6f');
dlmwrite('myfile.txt',NNN, 'coffset', 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
dlmwrite('myfile.txt',PPP, 'coffset', 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
dlmwrite('myfile.txt',QQQ, 'coffset', 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
dlmwrite('myfile.txt',RRR, 'coffset', 1, '-append', 'roffset', 1, 'delimiter', '\t', 'precision', '%.6f')
 
fid2=fopen('myfile.txt','at+');
fprintf(fid2,'\tMMM\n');
fprintf(fid2,'\tNNN\n');
fprintf(fid2,'\tPPP\n');
fprintf(fid2,'\tQQQ\n');
fprintf(fid2,'\tRRR\n');
fprintf(fid2,'MMM\n');
fprintf(fid2,'NNN\n');
fclose(fid2);
[code]


Problem and Question:
It is writing the last 7 lines correctly with the fprintf command. The first line with the number 2 and the second line with the numbers corresponding to a, b and nmat are not showing up with these numbers in them.i.e. they are just blank lines, as though the first 2 fprintf commands were't even there.

When I try those 2 commands separately with 2 different file names (myfile1.txt and myfile2.txt) it works correctly. But for myfile.txt, they don't.

Am I missing something with those first 2 fprintf commands?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 27 Jun, 2012 18:17:51

Message: 13 of 17

On 6/27/2012 12:37 PM, Eli wrote:
> Here's an updated version of my code with the all the above commands:
...

> fid1=fopen('myfile.txt','at+');
> fprintf(fid1,'\t2\n');
> fprintf(fid1,'\t %f %f %f\n', a, b, nmat);
> fclose(fid1);
>
...

> RRR = rand(x_coord,y-coord);
> dlmwrite('myfile.txt',MMM, 'coffset', 1, 'roffset', 2, 'delimiter',
> '\t', 'precision', '%.6f');
...
> fid2=fopen('myfile.txt','at+');
> fprintf(fid2,'\tMMM\n');
...
> fprintf(fid2,'NNN\n');
> fclose(fid2);
> [code]
>
>
> Problem and Question:
> It is writing the last 7 lines correctly with the fprintf command. The
> first line with the number 2 and the second line with the numbers
> corresponding to a, b and nmat are not showing up with these numbers in
> them.i.e. they are just blank lines, as though the first 2 fprintf
> commands were't even there.
>
> When I try those 2 commands separately with 2 different file names
> (myfile1.txt and myfile2.txt) it works correctly. But for myfile.txt,
> they don't.
>
> Am I missing something with those first 2 fprintf commands?

Nope--dlmwrite() isn't appending to the file, it's overwriting it and
simply putting in the number of blank lines requested at the beginning.

To prove this, try the following at the command line...

 >> fid=fopen('myfile.txt','wt');
 >> fprintf(fid,'\t2\n');
 >> fprintf(fid,'%f',pi);
 >> fid=fclose(fid);
 >> type 'myfile.txt'

2
3.141593
 >> dlmwrite('myfile.txt',rand(3),'\t',2,1)
 >> type myfile.txt



0.95013 0.48598 0.45647
0.23114 0.8913 0.018504
0.60684 0.7621 0.82141

 >>

You can either write the pieces in separate files and then concatenate
them via copy as a system command or write the whole thing via fprintf()

It's long enough ago now I forget the exact problem that prevented
dlmwrite() from being useful...oh--it was the mixture of character and
numeric data, wasn't it?

OK, you're stuck w/ forgetting dlmwrite() unless you do the
concatenation trick. AFAICR, a look-see at the current release doc's
online didn't show anything else high-level that will handle mixed data
types in a tab-delimited file.

You might search the file exchange if you haven't.

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 28 Jun, 2012 00:20:08

Message: 14 of 17

1. ^^^^^Okay, I printed the matrix with with fprintf using:

fprintf(fid, '\t%f \t%f \t%f \t%f \t%f \t%f\n', matrix);

This is for a matrix with 6 columns. However, as you saw from my previous post, I specified the number of columns as the variable "y_coord". In that post, y_coord was 61 so I would need to include \t%f 61 times in that fprintf statement.

Question 1:
Is there a way to tell fprintf to print \t%f a certain number of times (in this case, it would be specified by the variable y_coord)?

2. I am trying a different save the file with a (dlmwrite) file name that adds a ".1" to a file which dlmread() acted upon.

ex. dlmread read from file1.txt
dlmwrite is to write to file1.1.txt

To do this I used:
j=0.1;
I need to convert this to a string ".1".i.e. I have to drop (or remove) that leading zero.

Question 2:
How can i do this using fprintf?

3. I have written a simple code that does the following:
a. dlmread() read in all .txt files in the current directory
b. process them in some way
c. write them to .txt files, using dlmwrite()

There is no user input.i.e. just run the code and all .txt files in your current directory are processed.

I now need to convert this to a function and then to a GUI (or perhaps I could go straighttoa GUI, I don't know). I want the GUI to have one button - CONVERT. The user clicks this and all the .txt files in the current directory are converted and the converted content is written to text files. Other than performing the conversion, nothing else happens when the CONVERT button is clicked. However, with a single CONVERT button, there is no user input and the GUI does not display any putput, so I am not sure what the input and output of the function would be.

Question 3:
How would I set up this code as a function, if all it is doing is reading files, processing and then writing to files?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 28 Jun, 2012 00:52:01

Message: 15 of 17

On 6/27/2012 7:20 PM, Eli wrote:
> 1. ^^^^^Okay, I printed the matrix with with fprintf using:
>
> fprintf(fid, '\t%f \t%f \t%f \t%f \t%f \t%f\n', matrix);
>
> This is for a matrix with 6 columns. However, as you saw from my
> previous post, I specified the number of columns as the variable
> "y_coord". In that post, y_coord was 61 so I would need to include \t%f
> 61 times in that fprintf statement.
>
> Question 1:
> Is there a way to tell fprintf to print \t%f a certain number of times
> (in this case, it would be specified by the variable y_coord)?

There is, but it isn't in fprintf() -- unfortunately, C didn't include a
repeat in the format specification syntax so you have to build the
string dynamically...

fmt=[repmat('\t%f ',1,y_coord) '%\n'];
fprintf(fid,fmt,matrix);

Salt fmt to suit, obviously. Practice at the command line a little and
you'll get the hang of it altho it is a (what should be unnecessary)
pit{proverbial appendage} to have to do this way.

> 2. I am trying a different save the file with a (dlmwrite) file name
> that adds a ".1" to a file which dlmread() acted upon.
>
> ex. dlmread read from file1.txt
> dlmwrite is to write to file1.1.txt
>
> To do this I used:
> j=0.1;
> I need to convert this to a string ".1".i.e. I have to drop (or remove)
> that leading zero.
>
> Question 2:
> How can i do this using fprintf?

<http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F>

This example or one very much like it is in the current doc's too.

BTW, if you're going to do some sort of numbering convention like the
above, I strongly recommend using leading zeros or some other scheme so
that sorting files alphabetically "works" as expected.

> 3. I have written a simple code that does the following:
> a. dlmread() read in all .txt files in the current directory b. process
> them in some way
> c. write them to .txt files, using dlmwrite()
>
> There is no user input.i.e. just run the code and all .txt files in your
> current directory are processed.
>
> I now need to convert this to a function and then to a GUI (or perhaps I
> could go straighttoa GUI, I don't know). I want the GUI to have one
> button - CONVERT. The user clicks this and all the .txt files in the
> current directory are converted and the converted content is written to
> text files. Other than performing the conversion, nothing else happens
> when the CONVERT button is clicked. However, with a single CONVERT
> button, there is no user input and the GUI does not display any putput,
> so I am not sure what the input and output of the function would be.
>
> Question 3:
> How would I set up this code as a function, if all it is doing is
> reading files, processing and then writing to files?

As you described...I've never written a Matlab GUI, so I can't really
guide you other than to tell you to use the GUIDE tools. If it were me,
I'd simply use the UI**** packaged stuff like UIGETFILE, etc., to let
the user select a directory and then process it w/ a loop on the output
of dir(). I'd personally just type 'convert' or whatever the
function/script name is and call that good.

--

Subject: Writing Matrices to text file and printing additional text in

From: Eli

Date: 28 Jun, 2012 01:46:07

Message: 16 of 17

^^^^Thanks regarding the first two.

Regarding Q3:
(Ignoring the GUI part)
Just focusing on the (user-required) choice (i.e. user input) of current directory:
I'm really not familiar at all with how to incorporate this functionality. Why is the directory choice required? I am assuming that all the text files are in the current directory. Would this not be sufficient?

Subject: Writing Matrices to text file and printing additional text in

From: dpb

Date: 28 Jun, 2012 03:54:10

Message: 17 of 17

On 6/27/2012 8:46 PM, Eli wrote:
> ^^^^Thanks regarding the first two.
>
> Regarding Q3:
> (Ignoring the GUI part)
> Just focusing on the (user-required) choice (i.e. user input) of current
> directory:
> I'm really not familiar at all with how to incorporate this
> functionality. Why is the directory choice required? I am assuming that
> all the text files are in the current directory. Would this not be
> sufficient?

Well, yes, but it's a fairly limiting assumption as far as utility, but
if that's the case then sure...

--

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
gui Eli 27 Jun, 2012 20:24:08
specify f Eli 27 Jun, 2012 20:24:08
remove zero Eli 27 Jun, 2012 20:24:08
f Eli 27 Jun, 2012 20:24:08
t Eli 20 Jun, 2012 15:29:09
fopen Eli 20 Jun, 2012 15:19:09
fid Eli 20 Jun, 2012 15:19:09
cell data Eli 20 Jun, 2012 15:19:09
fprintf Eli 20 Jun, 2012 13:54:12
columns Eli 20 Jun, 2012 13:54:12
printing Eli 20 Jun, 2012 13:54:12
rows Eli 20 Jun, 2012 13:54:12
size Eli 20 Jun, 2012 13:54:12
formatting Eli 20 Jun, 2012 13:54:12
coffset Eli 20 Jun, 2012 13:54:12
roffset Eli 20 Jun, 2012 13:54:12
dlmwrite Eli 20 Jun, 2012 13:54:12
text Eli 20 Jun, 2012 13:54:12
matrices Eli 20 Jun, 2012 13:54:12
rssFeed for this Thread

Contact us