For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > output variables to single file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author output variables to single file
Jason

2008-01-14, 8:23 am


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


Krishna

2008-01-14, 8:23 am

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
Yumnam Kirani Singh

2008-01-14, 8:23 am

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 variab
les.
Anh Huy Phan

2008-01-14, 8:23 am

"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
Eyal Fleminger

2008-01-14, 8:23 am

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
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com