Lakshi
Lakshi

Reputation: 39

How to save a Matlab output in a text file

ans = 

    'C4'        ''      ''          'eighth note' 
    'C4'        ''      ''          'eighth note' 
    'C4'        ''      ''          'half note'   
    'G4'        ''      ''          'quarter note' 

This is the output of a matlab code of variable 'ans'. I want to save this result in a different text file. What are the steps should I follow ?

Thanks in advance.

Upvotes: 2

Views: 3856

Answers (2)

Yasser.AbdelSattar
Yasser.AbdelSattar

Reputation: 17

Starting in R2019a, you can use "writematrix" function to write a matrix to a file.

% M Your variable
% N your output

M = [C4,G4];
writematrix(M,N, "M.txt");

Upvotes: 0

Victor Marchuk
Victor Marchuk

Reputation: 13886

There are few functions that allow you to save the data to a file.

To save data to a file in a specifically formatted way, you can use fprintf:

x = 0:.1:1;
A = [x; exp(x)];

fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

To simply save workspace variables to a file you can use save:

p = rand(1,10);
q = ones(10);
save('pqfile.mat','p','q')

Upvotes: 0

Related Questions