user2532780
user2532780

Reputation: 23

Save matlab output to text file

I am running a script in Matlab to obtain a random permutations test of a matrix in order to obtain cross-validation accuracy values. My script is as follows:

%randperm 

labels = [zeros(40,1); ones(40,1)];

for i = 1:500
    p = labels(randperm(length(labels))); 
end


bestcv = 0; 
for log2c = -10:10, 
    for log2g = -10:10, 
        cmd = ['-s 0 -t 0 -v 20 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g) ' -q     ']; 
        cv = svmtrain(labels, p, cmd); 
        if (cv > bestcv),
            bestcv = cv; bestc = 2^log2c; bestg = 2^log2g;
            fprintf('%g %g %g (best c = %g, g = %g, rate = %g)\n', log2c, log2g, cv, bestc, bestg, bestcv);
        end
    end
end
cmd = ['-s 0 -t 0 -c ', num2str(bestc), ' -g ', num2str(bestg)];

I am wondering how I can save the output (500 cross-validation accuracy values) into a text file, and if it is possible to write this into my code.

Thanks in advance,

Andrea C

Upvotes: 2

Views: 249

Answers (2)

sietschie
sietschie

Reputation: 7553

I would suggest you to use save as Marc Claesen described it.

Nevertheless, if you just need a crude, fast way to somehow save your values, you could use matlabs diary command.

It saves all inputs and outputs to a textfile.

diary('cross-validation-output.txt') 

To stop writing to the file you need to call

diary OFF

Upvotes: 0

Marc Claesen
Marc Claesen

Reputation: 17026

You can save the variable(s) containing your cross-validation results using save and load them later using load. For example, assuming you have the results in the variable called accuracies:

save('cross-validation-results.txt',accuracies);

and later

load('cross-validation-results.txt');

to reobtain the variable accuracies.

To implement this in your code, save the tuning parameters and the associated accuracy into arrays and then save said arrays.

Upvotes: 0

Related Questions