Exporting different types of columns to a tab delimited txt file

Hello, I have a datafile with one column, and I need to create a txt file (tab delimited) with headers and three columns. Lets call the headers head1, head2 and head3. My data has to follow the next rules:
Under head1 I need to repeat the same string such as: 12/02/2012
Under head2 I need values 0:0.5:depends
Under head3 I need the data from my datafile
what I currently have is :
datalength = length(data) %calculate the number of rows
col1 = cell(lengthdata,1);
col1(:) = {'12/02/2012'} %this creates cells with the date
col2 = ones(lengthdata,1); %create a matrix for values
col3 = col2;
for row=1:datalenght %fill in col2 and col3
col2 = 0.5*row-0.5;
col3(row) = data(row)
end
As you can see I don't know how to join the columns, add the headers and export them to a tab delimited txt file.
Any help would be welcomed
David

 Risposta accettata

It’s difficult for me to follow your code, so I’m going with your description.
If you have R2013b or later, the easiest way would probably be to create a table (link), then use the writetable (link) function to create the file.
Experiment to get the result you want.

2 Commenti

Thanks for the answer, I have been able to do it, do you know if I can have a header with a space or how to modify the txt file to have one of the headers with a space?
My pleasure.
The column headers and row names of table objects must be valid MATLAB variable names. Spaces in the variable names are not permitted. The best you can do for a space is to use the underscore character ‘_’ in place of a space.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by