Reputation: 119
I am trying to print strings from an cell array and and numbers from a vector into a text file. I have to put the 'n th' string of the cell array and 'n th' number of vector in the 'n th' line of the text file. There will be a space in between these two things.
To do this, I converted vector into a cell and then concatenate two cells horizontally. However, I do not know how to add space in between. Still without that space that concatenated cell should print out something in the text file. However, it's not printing out. Any help!? Thanks!
Upvotes: 0
Views: 31
Reputation: 46
I think this sample code should solve your problem.
You get spaces by having spaces between your formats.
You need to use \r\n
to get a new line on Windows machines.
strings = {'hello','how','are','you'};
numbers = [1, 2, 3, 4];
fileID = fopen('tester.txt','w');
format = '%s %f \r\n';
for i = 1:length(numbers)
fprintf(fileID,format,strings{i},numbers(i));
end
fclose(fileID);
Upvotes: 2