2

in Matlab I have one 100X4000 mat which contains doubles and I have a 100X1 cell of chars. now the problem is how can I write all together in a CSV file. Matlab doesn't let me to do that. a shorter example is like this: first mat

0   1   2
0   0   0
0   5   3
9   0   7
0   4   0

second cell:

apple
banana
cherry
peach
other

the expected result (A) is:

0   1   2 apple
0   0   0 banana
0   5   3 cherry
9   0   7 peach
0   4   0 other

final goal is to do this:

csvwrite('dataLabels.csv',A);
6
  • 1
    Possible duplicate: stackoverflow.com/questions/14551153/… Commented Apr 24, 2015 at 0:18
  • @brodroll That answer is very inefficient. Also that's only half the question. This one has a numeric matrix too. Commented Apr 24, 2015 at 1:49
  • OP can you verify if my answer works for you and kindly accept if it's incorrect Commented Apr 24, 2015 at 1:57
  • @krisdestruction The one in the link provided requires writing numeric and char data to a csv, so they are the same in this aspect. The only difference is that here the data is in two different "containers" instead of a single cell array. Commented Apr 24, 2015 at 2:02
  • Yes so the problem exists from the OP's question of how to convert it to a numeric. The answer in the linked post is still pretty inefficient IMHO. Commented Apr 24, 2015 at 2:04

2 Answers 2

1

The code is as simple as follows:

% Toy Example Data
B = [0 1 2; 0 0 0; 0 5 3; 9 0 7; 0 4 0];
C = { 'apple' 'banana' 'cherry' 'peach' 'other' }';

B = num2str(B,'%i   %i   %i');
dlmwrite( 'dataLabels.csv', [B repmat( char(' '), length(C), 1 ) char( C{:} )], '' );

So what you want to do is change your matrix B into a char with your desired spacing. That's what the num2str function does with those parameters. Then you can use dlmwrite to delimit with no spacing. The code char( C{:} ) simply converts the cell into a char.

The code repmat( char(' '), length(C), 1 ) is just to put a space in between the matrix B and char cell C. Results

0   1   2 apple 
0   0   0 banana
0   5   3 cherry
9   0   7 peach 
0   4   0 other 
Sign up to request clarification or add additional context in comments.

2 Comments

You made me curious about how much more efficient this solution is! But I can already tell this is indeed more efficient since you eliminated the need of 2 for loops. Nice one +1
Thank you :) The solution you linked did indeed work for both numerics and cells, but as you can see, everyone resorts to the double loop and this is a very vectorized solution. I hate clunky code XD
0

You can use fopen(filepath, w) and fprintf(fileId, data) to write several matrices as strings in a csv file.

Example:

function createCSV(name, data1, data2)
fileId = fopen(path/to/file.csv, 'w');
[x, y] = size(data);

for i = 1:x
    for j = 1:y
        fprintf(fileId, num2str(data(x, y)));
        fprintf(fileId, ',');
    end
    fprintf(fileId, num2str(data2(x)));
    fprintf(fileId, '\n');
end

end

fopen, fprintf

1 Comment

Your code is very inefficient due to the double loop :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.