How can I write to a CSV file in separated columns and without any commas?

I am using the following code to write in a CSV file for 10 different columns but I end up with all data in a single column and commas between each data.
dlmwrite('/Users/.../matlabdandata2.csv',[frames' time' cycle' ...],'delimiter',',','-append')
Thanks!

Risposte (3)

csvwrite('matlabdandata2.csv', [frames, time, cycle])

9 Commenti

It gives this error:
Undefined function 'write' for input arguments of type 'double'.
Error in writetable (line 124)
write(a,filename,varargin{:})
Sorry, I had the wrong command. See my edit above.
Thank you very much for your answer but it still writes all data to a single column.
%'Enter the file name: ' is /Users/ecesuildiz/Desktop/denemekod/book1.xls
%'Enter actuation frame: ', 's' is 141
%'Enter the number of best frames: ' is 40
my_str = cell(1,3);
my_str{1} = input('Enter the file name: ', 's');
my_str{2} = input('Enter actuation frame: ', 's');
my_str{3} = input('Enter the number of best frames: ', 's');
file = xlsread(my_str{1},'Results');
act_frame = str2double(my_str{2});
index = str2double(my_str{3});
frames = file(1:index);
mean1 = file(index+1:2*index); %cytop
mean2 = file(2*index+1:3*index); %backgr1
mean3 = file(3*index+1:4*index); %backgr2
mean4 = file(4*index+1:5*index); %backgr3
avgBack = (mean2+mean3+mean4)/3;
cyto_minus_backgr = mean1-avgBack;
normalized = cyto_minus_backgr.*(1/cyto_minus_backgr(1));
fps = 4.9;
time = zeros();
for i = 1:index
time(i) = (frames(i)-act_frame)/fps;
end
timenot0 = find(time>=0,1,'first');
cycle=zeros();
cycle(timenot0)=2;
for i = timenot0:index-1
cycle(i+1)=cycle(timenot0)+cycle(i);
end
csvwrite('/Users/ecesuildiz/Desktop/matlabdandata2.csv',[frames', time', cycle'])
Here all arrays contain double and index is 40 (taken from user). Thank you very much.
Using this code and the data file you provided below results in the following which appears to be in separate columns.
But this answer gives commas between the values. Just open the csv file in a text editor and you'll see, and for some weird reason the poster didn't want commas and specifically said so. So he shouldn't use the csv extension and should use a .dat or .txt extension, and call dlmwrite() like I did in my answer.
Also, where did you get his input data with frames, times, cycles, etc.? I don't see that he attached it.
That workbook has only 5 columns and they don't even have names that match. How are we supposed to generate the variables he called "[frames' time' cycle' ...]" which he is trying to stitch together?
And does he want 3 columns in the output text file, or 10 columns? And what does "the only problem is separating as 10 different columns" mean?

Accedi per commentare.

When you did this:
dlmwrite('/Users/.../matlabdandata2.csv', [frames' time' cycle' ...], 'delimiter', ',', '-append')
did you notice that you specified a comma as the delimiter? Why?
If you don't want a comma, specify a space or something else as the delimiter.

2 Commenti

Thanks you are right. Now the only problem is separating as 10 different columns.
What does this say in the command window:
data = [frames', time', cycle', ...etc....] % Stitch 10 vectors side by side.
whos data
dimensions = size(data)
Attach your data if you want people to help you.

Accedi per commentare.

It gives the following;
data =
113.0000 -5.7143 0
119.0000 -4.4898 0
124.0000 -3.4694 0
129.0000 -2.4490 0
139.0000 -0.4082 0
154.0000 2.6531 2.0000
169.0000 5.7143 4.0000
184.0000 8.7755 6.0000
199.0000 11.8367 8.0000
214.0000 14.8980 10.0000
229.0000 17.9592 12.0000
244.0000 21.0204 14.0000
259.0000 24.0816 16.0000
273.0000 26.9388 18.0000
289.0000 30.2041 20.0000
304.0000 33.2653 22.0000
318.0000 36.1224 24.0000
332.0000 38.9796 26.0000
349.0000 42.4490 28.0000
363.0000 45.3061 30.0000
379.0000 48.5714 32.0000
393.0000 51.4286 34.0000
408.0000 54.4898 36.0000
423.0000 57.5510 38.0000
438.0000 60.6122 40.0000
453.0000 63.6735 42.0000
467.0000 66.5306 44.0000
483.0000 69.7959 46.0000
498.0000 72.8571 48.0000
513.0000 75.9184 50.0000
527.0000 78.7755 52.0000
542.0000 81.8367 54.0000
558.0000 85.1020 56.0000
573.0000 88.1633 58.0000
588.0000 91.2245 60.0000
602.0000 94.0816 62.0000
618.0000 97.3469 64.0000
632.0000 100.2041 66.0000
647.0000 103.2653 68.0000
663.0000 106.5306 70.0000
Name Size Bytes Class Attributes
data 40x3 960 double
Just used first 3 column. XLS file is also attached. Thank you very much.

6 Commenti

Please attach your input data with the 10 variables frames, times, cycles, etc. I don't see that you attached it.
I solved the problem of how to get rid of the comma for you, but with "the only problem is separating as 10 different columns", I can't do that unless I have your 10 variables or 10 columns.
What are you stitching together that is giving you 10 columns and describe what "separating as 10 different columns" looks like to you. What kind of output do you want from "separating as 10 different columns"?
I got this result:
But, I want to obtain this result (I don't know why it gives MATLAB data in this form there are comma and dot errors);
Correct excel file is attached. Actuation frame is 102 and number of best frames is 44. Thank you very much Sir.
What are the rules for when comma should be used as the decimal separator or period should be used for the decimal separator ? You use both in several of the columns.
Just a dot will be enough as decimal separator. However here, I got the numbers also wrong. They should be like that;
When you said "separator" regarding a CSV (Comma Separated Variable) file I assumed you meant the separator between the complete numbers. Now I'm wondering if you're talking about the separator between the thousands and millions groups in the number instead. Which is it?
Also I'm wondering if the columns in your table are actually strings, because I don't see why, in your first screenshot, cell F1 uses a comma but cell F2 uses a period for the decimal point.
What is your "region" (country) that your computer is set up for in your settings/control panel? Is it one of those countries that uses commas instead of dots for decimal points?
I meant the separation of columns by saying "CSV file in separated columns". Also I am talking about the dot separator (in each cell) as decimal point separator. I am trying to obtain the exact output as shown in MATLAB matrix and I don't want any commas. It is set up for Turkey and Turkey uses commas for decimal points. Maybe that's the reason. I'll check. Thank you again Sir.

Accedi per commentare.

Richiesto:

il 13 Lug 2018

Commentato:

il 15 Lug 2018

Community Treasure Hunt

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

Start Hunting!

Translated by