% Sample file to demonstrate how to export data from Matlab to other
% applications

% This file generates a text file using the FPRINTF function in Matlab
% A. Trani (October 5, 2013)

clear

% Import some data (bridges of the world file)

% Name  	Country  	Completed  	 Length (m) 
% Mackinac	United States	1957	8038 
% Xiasha	China	1991	8230 

% Read the complete Excel file (previous exercise)

[num,txt,raw] = xlsread('bridges_of_the_world_short.xls','Bridge data');

% Create individual variables with names: NOTE: convert cell array to
% string array (equal length)

noRows = length(raw);           % 23 entries for this case

bridgeName      = char(raw{2:noRows,1});         % converts cell array into a string array in Matlab - 
                                                                        % saves the result into variable bridgeName
bridgeCountry   = char(raw{2:noRows,2});        % saves result into string array bridgeCountry
bridgeYear         = num(:,1);                              % no need to convert - values are all numeric
bridgeLength     = num(:,2);                             % no need to convert - values are all numeric

% Create a text file (called myOutout.txt) and write all four output
% variables (bridge name, country, year completed and length)

for i=1:noRows-1                                                       % loop to write output
        fid = fopen ('myOutput.txt','a');                           % create a text file
        fprintf(fid, '%s %s %f %f \n',bridgeName(i,:), bridgeCountry(i,:),  bridgeYear(i),  bridgeLength(i));
        status = fclose(fid);
end
