# I have 10 rows and four columns in variable two. I want to write one row in one cell, 2nd row in 2nd cell, 3rd row in 3rd cell and so on into an Excel file using Matlab. How can I do this?

1 view (last 30 days)
Sadiq Akbar on 17 Oct 2019
Commented: Sadiq Akbar on 18 Oct 2019
I have a Matrix in Matlab Workspace having floating point data. The order of the matrix is 10x4. I want to write 1st row in A1 cell, 2nd row in A2 cell, 3rd row in A3 cell and so on into an Excel file using Matlab. How can I do that?
Regards,

#### 1 Comment

Guillaume on 17 Oct 2019
Why on earth do you want to do this? Excel does not support having several numbers in a cell, so the only way it can be done is by writing the data as text in the cell (easily done if needed) which would make the data useless for further processing in excel.

xi on 17 Oct 2019
use num2str() to convert your 10X4 numeric matrix into 10 strings.
Then use xlswrite() to write string into Excel cells.
Here's an example
filename='myfile.xlsx';
M=rand(10,4);
str=num2str(M);
for i=1:size(str,1)
xlswrite(filename, cellstr(str(i,:)), 'Sheet1', ['A',num2str(i)]);
end

Show 2 older comments
Sadiq Akbar on 17 Oct 2019
Thank you very much for your prompt response xi. No no, I don't want to sort the row. I think I should re-phrase my question.
I have a desired vector called u and is given by u=[0.5000 1.0000 1.0121 2.011]. I am estimating it using an algorithm. With the help of algorithm, I get 10 vectors having values approximately like u. But the problem is that sometimes the algorithm gives me estimated vector in which the position of element1 is swapped with element2. Likewise, the position of element3 is swapped with element4 which I don't want. I want to get the estimated vector in such a way in which there occures no such swapping. e.g. I get estimated vectors like these:
Estimated=[1.0010 0.5001 2.0011 1.0012]
=[0.5010 1.0101 1.0101 2.0101]
=[1.0110 0.4999 1.9999 1.0001]
------------------------------------------
-------------------------------------------
Comparing these with the desired u, we see that In the 1st Estimated vector, the position of element 1 is swapped with element2. Likewise the position of element3 is swapped with element4. I don't want swapping in estimated vectors like this. I want to get estimated vector in the same order as u always.
Regards,
xi on 18 Oct 2019
I don't get what is the hard part that prevent you from realizing it?
Just make a judgement of whether it is swapped or not and then process it accordingly.
There are many ways you can choose to make such judgement: for example, you can do a pearson correlation between the estimated and the desired, for all the possible mutants upon swapping, and then choose the one with the hightest correlation score.
Xi
Sadiq Akbar on 18 Oct 2019
Thank you very much Xi for your interactive responses. And yes, I solved the problem. Thank you once again.
Regards,