Write vectors to a file in Matlab?

Status
Not open for further replies.

davyzhu

Advanced Member level 1
Joined
May 23, 2004
Messages
494
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,298
Location
oriental
Activity points
4,436
Hello all,

How to Write vectors to a file in Matlab? And read from the file? The easiest way?
For example, A=[1 3 5 7 9],B=[2 7 6 8 9 10 11 12],C= ...and I want to write/read these vectors to a file, thanks!

Regards,
Davy
 

I think the easiest way is use the "load" and "save" commands

To save a vector x and a vector y to a file names "Myfile" do the following:

save 'Myfile' x y

Note that you can save any number of vectors to the same file, just separate them by a space

To load those vectors into workspace use:

load 'Myfile' x y


Hope this is useful
 

    davyzhu

    Points: 2
    Helpful Answer Positive Rating
use fprintf

example:

x = 0:.1:1; y = [x; exp(x)];
fid = fopen('exp.txt','w'); %file's name mode write
fprintf(fid,'%6.2f %12.8f\n',y); %write in file
fclose(fid);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…