how to create a txt file of output for a program in Matlab s
how to create a .txt file of output for a program in Matlab \"sprintf\"??????????????????
The out I am getting from my function for finding the roots of a quadratic formula needs to be saved and printed in a .txt file..............
Copy of my formula
function [ root1,root2 ] = quadratic_formula( a,b,c )
% Design a Matlab using the algorithm for solving quadratic equations
% Prompt the user to enter the coefficients
% ax^2+bx+c = 0
a(1) = input(\'Please enter a value for (a) >> \');
b(1) = input(\'Please enter a value for (b) >> \');
c(1) = input(\'Please enter a value for (c) >> \');
if a == 0
disp(\'Error: Value Will Result In An Undefined Answer\')
disp(\'PLEASE ENTER A VALUE > 0\')
end
if b^2-4*a*c > 0
root1 = (-b+sqrt(b^2-4*a*c))/2*a; % Root of the equation
root2 = (-b-sqrt(b^2-4*a*c))/2*a;
elseif b^2-4*a*c == 0;
root1 = -b/2*a;
disp(\'There is only one root\')
else
disp(\' Error: Imaginary Roots\')
disp(\'No Real Solution\')
end
end
Solution
fprintf is used to save a data to .txt file in MATLAB.
first you need to open a file in write mode with a file handler like
myfile = fopen(\'File.txt\', \'w\');
where w indicates file with file handler myfile and filename File.txt is in write mode.
now when you want to save the data to File.txt use fprintf as below:
it will save the value of B in File.txt
and than close the file using file handle as below:
fclose(myfile);
now for your MATLAB program find the updated program as below:
function [ root1,root2 ] = quadratic_formula( a,b,c )
% Design a Matlab using the algorithm for solving quadratic equations
% Prompt the user to enter the coefficients
% ax^2+bx+c = 0
% open File.txt with handle myfile in write mode
myfile = fopen(\'File.txt\', \'w\');
a(1) = input(\'Please enter a value for (a) >> \');
b(1) = input(\'Please enter a value for (b) >> \');
c(1) = input(\'Please enter a value for (c) >> \');
if a == 0
disp(\'Error: Value Will Result In An Undefined Answer\')
disp(\'PLEASE ENTER A VALUE > 0\')
end
if b^2-4*a*c > 0
root1 = (-b+sqrt(b^2-4*a*c))/2*a; % Root of the equation
root2 = (-b-sqrt(b^2-4*a*c))/2*a;
%save values of root1 and root2 in File.txt
fprintf(myfile,\'%0.4f\ \',root1);
fprintf(myfile,\'%.4f\ \',root2);
elseif b^2-4*a*c == 0;
root1 = -b/2*a;
%save values of root1 in File.txt
fprintf(myfile,\'%.4f\ \',root1);
disp(\'There is only one root\')
else
disp(\' Error: Imaginary Roots\')
disp(\'No Real Solution\')
end
end
fclose(myfile);
or you can also do like this in which every display statement is also in File.txt
function [ root1,root2 ] = quadratic_formula( a,b,c )
% Design a Matlab using the algorithm for solving quadratic equations
% Prompt the user to enter the coefficients
% ax^2+bx+c = 0
% open File.txt with handle myfile in write mode
myfile = fopen(\'File.txt\', \'w\');
pqr1 = \'Error: Value Will Result In An Undefined Answer\';
pqr2 = \'PLEASE ENTER A VALUE > 0\';
pqr3 = \'There is only one root\';
pqr4 = \' Error: Imaginary Roots\';
pqr5 = \'No Real Solution\';
a(1) = input(\'Please enter a value for (a) >> \');
b(1) = input(\'Please enter a value for (b) >> \');
c(1) = input(\'Please enter a value for (c) >> \');
if a == 0
%save display statement in File.txt
fprintf(myfile,\'%s\ \',pqr1);
fprintf(myfile,\'%s\ \',pqr2);
end
if b^2-4*a*c > 0
root1 = (-b+sqrt(b^2-4*a*c))/2*a; % Root of the equation
root2 = (-b-sqrt(b^2-4*a*c))/2*a;
%save values of root1 and root2 in File.txt
fprintf(myfile,\'%0.4f\ \',root1);
fprintf(myfile,\'%0.4f\ \',root2);
elseif b^2-4*a*c == 0;
root1 = -b/2*a;
%save values of root1 in File.txt
fprintf(myfile,\'%0.5fn\',root1);
%save display statement in File.txt
fprintf(myfile,\'%s\ \',pqr3);
else
%save display statement in File.txt
fprintf(myfile,\'%s\ \',pqr4);
fprintf(myfile,\'%s\ \',pqr5);
end
end
fclose(myfile);
note %.4f will display result upto 4 decimal places.


