Thread Subject: output variables to single file

Subject: output variables to single file

From: Jason

Date: 14 Jan, 2008 11:54:02

Message: 1 of 5


Hello, I would like to output MATLAB variables to a single
file - not a .mat file My problem is that I am designing a
GUI and it would be neater to have a single file rather than
loading a .mat file with a number of variables.

So my problem is that I have

Time
0
1
2
3
4
5

Velocity
0
2
4
6
8
10

At the moment I am saving them to the workspace as a .mat
file. However what I want to do is write both of them to a
file.txt so I have

Time Velocity
0 0
1 2
2 4
3 6
4 8
5 10

I have little experience of MATLAB so can anyone direct me
to any pointers on doing this? I've tried the usual searches
but I think my keywords are too vague.

Thanks


Subject: output variables to single file

From: Krishna

Date: 14 Jan, 2008 12:21:39

Message: 2 of 5

On Jan 14, 4:54 pm, "Jason " <j_henderso...@REMOVEhotmail.com> wrote:
> Hello, I would like to output MATLAB variables to a single
> file - not a .mat file My problem is that I am designing a
> GUI and it would be neater to have a single file rather than
> loading a .mat file with a number of variables.
>
> So my problem is that I have
>
> Time
> 0
> 1
> 2
> 3
> 4
> 5
>
> Velocity
> 0
> 2
> 4
> 6
> 8
> 10
>
> At the moment I am saving them to the workspace as a .mat
> file. However what I want to do is write both of them to a
> file.txt so I have
>
> Time Velocity
> 0 0
> 1 2
> 2 4
> 3 6
> 4 8
> 5 10
>
> I have little experience of MATLAB so can anyone direct me
> to any pointers on doing this? I've tried the usual searches
> but I think my keywords are too vague.
>
> Thanks

use fprintf
from help fprintf

    Create a text file called exp.txt containing a short table of the
    exponential function. (On Windows platforms, it is recommended
that
    you use FOPEN with the mode set to 'wt' to create a text file for
    writing.)

        x = 0:.1:1; y = [x; exp(x)];
        fid = fopen('exp.txt','wt');
        fprintf(fid,'%6.2f %12.8f\n',y);
        fclose(fid);

    Now examine the contents of exp.txt:

        type exp.txt
           0.00 1.00000000
           0.10 1.10517092
                ...
           1.00 2.71828183

Krishna
~ Blog on Digital Signal Processing, http://www.dsplog.com

Subject: output variables to single file

From: Yumnam Kirani Singh

Date: 14 Jan, 2008 12:23:22

Message: 3 of 5

You can easily write your output to a file using fprintf or dlmwrite. But I would rather suggest you to save all the varialbles in a MAT file, that will be much more handy than reading the data from the file and then subsequently assign the data to variables.

Subject: output variables to single file

From: Anh Huy Phan

Date: 14 Jan, 2008 12:43:03

Message: 4 of 5

"Jason " <j_henderson44@REMOVEhotmail.com> wrote in message
<fmfigq$f3v$1@fred.mathworks.com>...
>
> Hello, I would like to output MATLAB variables to a single
> file - not a .mat file My problem is that I am designing a
> GUI and it would be neater to have a single file rather than
> loading a .mat file with a number of variables.
>
> So my problem is that I have
>
> Time
> 0
> 1
> 2
> 3
> 4
> 5
>
> Velocity
> 0
> 2
> 4
> 6
> 8
> 10
>
> At the moment I am saving them to the workspace as a .mat
> file. However what I want to do is write both of them to a
> file.txt so I have
>
> Time Velocity
> 0 0
> 1 2
> 2 4
> 3 6
> 4 8
> 5 10
>
> I have little experience of MATLAB so can anyone direct me
> to any pointers on doing this? I've tried the usual searches
> but I think my keywords are too vague.
>
> Thanks
>
>

Two links may help you export data in ASCII formats

http://www.mathworks.com/access/helpdesk/help/techdoc/
matlab_prog/f5-15544.html

http://www.mathworks.com/access/helpdesk/help/techdoc/
matlab_prog/f5-84430.html#f5-6458


Time = 0:5;
Velocity = 0:2:10;
data = [Time Velocity]';

fid = fopen('dataTV.txt','w');
fprintf(fid,sprintf('Time %s Velocity\n',blanks(3)));
fprintf(fid,'%4d %8d\n',data);
status = fclose(fid);

Anh Huy Phan
RIKEN - BSI

Subject: output variables to single file

From: Eyal Fleminger

Date: 14 Jan, 2008 12:49:57

Message: 5 of 5

Assuming you have two column vectors T and V with the data:

If you just want the data, you can use (delimited by spaces)

A=[T,V];
save 'file.txt' A -ascii

or

A=[T,V];
csvwrite('file.txt',A)

for comma-seperated values

If you also want headers, it's a bit more complicated; try dlmwrite or
fprintf.

"Jason " <j_henderson44@REMOVEhotmail.com> wrote in message
news:fmfigq$f3v$1@fred.mathworks.com...
>
> Hello, I would like to output MATLAB variables to a single
> file - not a .mat file My problem is that I am designing a
> GUI and it would be neater to have a single file rather than
> loading a .mat file with a number of variables.
>
> So my problem is that I have
>
> Time
> 0
> 1
> 2
> 3
> 4
> 5
>
> Velocity
> 0
> 2
> 4
> 6
> 8
> 10
>
> At the moment I am saving them to the workspace as a .mat
> file. However what I want to do is write both of them to a
> file.txt so I have
>
> Time Velocity
> 0 0
> 1 2
> 2 4
> 3 6
> 4 8
> 5 10
>
> I have little experience of MATLAB so can anyone direct me
> to any pointers on doing this? I've tried the usual searches
> but I think my keywords are too vague.
>
> Thanks
>
>


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
eporting saving data Jason 14 Jan, 2008 06:55:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com