MATLAB Answers

0

How do I convert a table to .csv format

Asked by Charles on 7 Jun 2019
Latest activity Answered by Star Strider
on 7 Jun 2019
I have the follwing code, and i wish to convert a table to a .csv format and not a .txt as this code does. The inputfile is a number of tables.
How can i modify this code?
Data = load('INPUTFILEedit.mat');
DataName = fieldnames(Data);
for k = 1:numel(DataName)
ThisName = DataName{k};
ThisData = Data.(ThisName);
tst=ThisData.Date(1:height(ThisData(2:end,2))); % just a sample set to play with
mydates = datetime(tst,'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSSSSSSSSZ ', ...
'TimeZone','Europe/London','Format','y-MM')
% size(AUDCAD)
ThisData = horzcat(num2cell(mydates), ThisData(2:end,5));
% ThisData.New=ThisData.Close; % make a new column with content of close
% writetable(ThisData,'ThisData','WriteVariableNames',0) % write a text file without header ...
OutputName = [strrep(ThisName, '_', ''),'1d'];
writetable(ThisData,OutputName,'WriteVariableNames',0)
end

  0 Comments

Sign in to comment.

Tags

Products


Release

R2018a

1 Answer

Answer by Star Strider
on 7 Jun 2019

Note that .csv files are text files. If you want to save it specifically as a .csv file, tell writetable by including the extension with the filename:
OutputName = [strrep(ThisName, '_', ''),'1d.csv']
I only created and wrote the first one because I didn’t want to have to delete all of them.
So:
AUDCAD1d.csv
exists as:
2016-06,0.98484
2016-07,1.00445
2016-08,1.01999
2016-09,0.99191
2016-10,0.96691
2016-11,0.9879
2016-12,1.01805
2017-01,1.01495
2017-02,1.02145
2017-03,1.00287
2017-04,0.9957
2017-05,0.9983
2017-06,0.99159
2017-07,0.97589
2017-08,0.98672
2017-09,0.97553
2017-10,0.98113
2017-11,0.9918
2017-12,0.99582
2018-01,0.99013
2018-02,0.96672
2018-03,0.98026
2018-04,0.97194
2018-05,0.96594
2018-06,0.93691
2018-07,0.93166
2018-08,0.93092
2018-09,0.97011
2018-10,0.96035
2018-11,0.95445
2018-12,0.93405
2019-01,0.94632
2019-02,0.94328
2019-03,0.93704
2019-04,0.93225
and appears to me to be a comma-delineated file.

  0 Comments

Sign in to comment.