I have a file 'test_file.txt' which contains:
some matrix x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 var1 99 88 55 23 27 88 79 99 52 66 71 81 15 5 6 7 2811562569 999 2593699 759 8561245 999 123 58 62 12 9
Each column in the matrix (i.e. x10, x11,…) can have up to 4 numeric values, the formating the file is written in does not have a clean delimeter throughout. How can I read in data and have my code specify a maximum of 4 values in each column?
Here is what I've got:
readfile = 'testfile.txt';writefile = 'my_file.txt';summaryFile = fopen(writefile,'wt');fid = fopen(readfile,'r');format_str = repmat('%4.0d',1,31);header = fgetl(fid);fprintf(summaryFile, '%s\n', header);tline = fgetl(fid);tmp2 = textscan(tline, ['%q ' format_str '\n']);var = cell2mat(tmp2{1});vals = cell2mat(tmp2(:,2:end));fprintf(summaryFile, [' %s ' format_str], var, vals);
and this is what I get from the above code:
some matrix x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 var1 99 88 55 23 27 88 79 99 52 66 71 81 15 5 6 728115625 69 9992593 699 7598561 245 999 123 58 62 12 9
What I want to be outputted is something like this:
some matrix x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 var1 99 88 55 23 27 88 79 99 52 66 71 81 15 5 6 7 28 1156 2569 999 259 3699 759 856 1245 999 123 58 62 12 9
Thanks in advance.
Best Answer