%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB. Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I read data from a external data text file?
%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/dataread.m;
% http://numericalmethods.eng.usf.edu/blog/vectordata.txtx;
% Last Revised: November 29, 2009;
% Abstract: This program shows you how to read from
% an external data file. .

%% PROGRAM
clc
clear all
clf
filen='w:/blog/vectordata.txt';
fh=fopen(filen,'r');
% r to read
% w to write
% a to append

% INPUTS
% fgets gets the data from the data file
A_vector=fgets(fh);
% sscanf parses the read data into a vector A 
A=sscanf(A_vector,'%f');

B_vector=fgets(fh);
B=sscanf(B_vector,'%f');

disp('The vector A is')
disp(A)
disp('The vector B is')
disp(B)

n=length(A);
%finding the dot product
dot_product=0;
for i=1:1:n
dot_product=dot_product+A(i)*B(i);
end
fprintf('\n The dot product is =%g',dot_product)