how to store values in csv format?

I have 1x500 matrix with doubles. I want to save it in a CSV file format
matrix=[a,b,c,d ,data];
dlmwrite('matrix.csv',matrix,'-append',delimiter,',') %saving data
the data is getting saved in excel sheet from 2nd row .
is there any way to save data from first cell of excel sheet?

 採用された回答

Voss
Voss 2022 年 3 月 27 日

0 投票

Maybe try it without the '-append' option (and ',' is the default delimiter, so you don't need to specify it [and 'delimiter' needed to be in quotes anyway]):
dlmwrite('matrix.csv',matrix)
Also note that the documentation for dlmwrite recommends using writematrix instead.

4 件のコメント

ramya
ramya 2022 年 3 月 27 日
from loop i will always get 1x500 matrix values
and each row will be of 1x500 and i have to save each row data in a CSV file format without overlappingg each other's data..
Voss
Voss 2022 年 3 月 27 日
I see. In that case either one of these approaches will work:
  1. Write the first line without '-append' and then all remaining lines with '-append'
  2. Accumulate all rows into a matrix and write it to file one time at the end (without '-append')
ramya
ramya 2022 年 3 月 27 日
Sorry sir , but i am not able to understand...
Voss
Voss 2022 年 3 月 27 日
Your code might look like this (Option 1 above):
% you have a 1x500 matrix with doubles, called matrix
dlmwrite('matrix.csv',matrix); % write it. this is the first row
for ii = 2:N_rows % for the remaining rows
matrix = randn(1,500); % you get the next 1x500 row of numbers somehow
dlmwrite('matrix.csv',matrix,'-append'); % write it, appending to what's already in the file
end
Or your code might look like this (Option 2 above):
% you have a 1x500 matrix with doubles, called matrix
% build another matrix that will contian all the rows, call it all_matrix
all_matrix = matrix; % at first, it only has the first row
for ii = 2:N_rows % for the remaining rows
matrix = randn(1,500); % you get the next 1x500 row of numbers somehow
all_matrix(end+1,:) = matrix; % and add the new row to the end of all_matrix
end
% now that all_matrix has all the rows, write it to file:
dlmwrite('matrix.csv',matrix);

サインインしてコメントする。

その他の回答 (0 件)

タグ

質問済み:

2022 年 3 月 27 日

コメント済み:

2022 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by