yliueagle
yliueagle

Reputation: 1201

save Matlab output to file, instead of displaying on screen

I use some optimization function and it displays results of iteration steps to the screen. How can I save the iteration results to file, instead of displaying them on the screen?

I referred to fprintf but it doesn't work in my case. Also diary does not work, because I do not want to display on screen.

Many thanks.

Upvotes: 1

Views: 1406

Answers (2)

Navan
Navan

Reputation: 4477

Use evalc when you call your function to capture its output.

T = evalc('string to evaluate')

This will capture all the output strings which would normally be printed in the command window. You can then write T to a file.

Upvotes: 0

Vuwox
Vuwox

Reputation: 2359

Simple thing,

create a logFile. Start your function with :

fid = fopen(logFile,'w+');

When you want to display something you can do :

fprintf(fid,'This is my result %d.\n',100); % Will print in the file
fprintf('This is my result %d.\n',100); % Will print on screen

And remember at the end of your function to close it :

fclose(fid);

Upvotes: 3

Related Questions