I am having trouble writing my matrix to a text file.

10 views (last 30 days)
I am trying to write my variable, textfiledata, to a text file. textfiledata is a nx4 matrix where n changes each iteration. I added labels to each of the four columns using:
header = {'time' 'A1' 'A2' 'INT'};
textfiledata=[header;num2cell(textdataWITHOUTheader)];
I am not sure how to save this matrix to a text file to a designated directory. i tried using save but ran into ASCII errors. I have also tried dlmwrite with no luck.
Thank you for your help.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Nov 2017
Edited: Walter Roberson on 2 Nov 2017
fid = fopen('OutputFileNameGoesHere.txt', 'wt');
tdflip = textfiledata .';
fprintf('%s\t%s\t%s\t%s\n', tdflip{:});
fclose(fid);
  6 Comments
Matt Mallory
Matt Mallory on 2 Nov 2017
tested the outcome, still no luck.
Thanks for the help though!
Walter Roberson
Walter Roberson on 2 Nov 2017
You are doing num2cell on a variable named "textdataWITHOUTheader". That would be a valid call to make on a char array, if the desired result were one character per cell, but I have to wonder whether textdataWITHOUTheader is perhaps numeric instead of a char array? If it is numeric then we would need to format differently.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!