MATLAB: Exporting numerical and string values to *.txt file (in batch)

exportexport delimitedfprintfnumericalsave delimitedsave resultssave to txtstringstxt file

Hi, I am having difficulties with using fprintf to export numerical and string data to a *.txt file.
In my algorithm, I import in batch mode many *.txt files (tab delimited) including numbers, each file looking like the image below. The number of columns is always the same, but the number of rows vary each time and I have no info on this :
The problem with fprintf is that it prints columns as rows, while I needed it to keep the form of the above table. I managed to make it work with the code below, by concatenating all the numerical Result columns togeter into an array, which I then print as shown.
% Batch Processing all files
for j = 1:numel(txt_files)
[PathName, filename, ext] = fileparts(char(txt_files(j).name));
%%Initialize variables.
filename = txt_files(j,1).name;
delimiter = '\t';
startRow = 2;
%%Format string for each line of text:
formatSpec = '%d%d%6.2f%6.2f%6.2f%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,... 'HeaderLines',startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Allocate imported array to column variable names
Result01 = dataArray{:, 1};
Result02 = dataArray{:, 2};
Result03 = dataArray{:, 3};
Result04 = dataArray{:, 4};
Result05 = dataArray{:, 5};
%%Clear temporary variables
clearvars delimiter startRow formatSpec fileID dataArray ans;
%%Create txt array with pathname, need as many rows as Result instances %(length(Result01))
SelectedName = repmat(char(filename), length(Result01), 1);
%%Write results in file (append)
All_Results = cat(2, Result01, Result02, Result03, Result04, Result05);
fprintf(fid_Destinfile, [repmat('%f\t', 1, size(All_Results, 2)) '\n'], All_Results');
end
My problem is that I need to add another column in each file iteration, which would include a string with the current pathname (This would be the same pathname for every row within the same file, I have created it here under the name SelectedName) So I would like the exported data to look as in the image below (again tab-delimited) and for each file iteration, to keep adding results in new rows.
I have tried converting all the data (numerical and strings) into strings,so that I could put everything together in a string array, but I could not print them as wanted. I also tried putting everything in a cell, but I could not manage to print from within the cell in the right order, as I couldn't find a way to make the last line of my code applicable for cells.
Any help would be greatly appreciated.
Thanks, ziggy

Best Answer

  • Making this way harder than needs be...
    fmtOut = [repmat('%6d\t',1,2) repmat('%6.2f\t',1,3) '%s\n'];
    % Batch Processing all files
    for j = 1:numel(txt_files)
    data=textread(txt_files(j).name,'','headerlines',1);
    fidOut=fopen('YourOutputFileNameHere','w');
    for k=1:size(data,1)
    fprintf(fidOut,fmtOut,data(k,:),txt_files(j).name);
    end
    fidOut=fclose(fidOut);
    end
    While deprecated textread has many benefits for simple files; it returns a "regular" double array, not cell, it infers the proper number of columns and shape with the empty format string option automagically, and doesn't need the fopen/fclose hoopla.
    After that, simply create a desirable output file name (I would recommend to NOT overwrite existing files at least until you've thoroughly debugged the process) and loop thru appending the desired output string onto each line.