Simple MATLAB to Excel Question

  • Context: MATLAB 
  • Thread starter Thread starter swartzism
  • Start date Start date
  • Tags Tags
    Excel Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB user seeking assistance with exporting a 51x3 matrix to an Excel sheet while including column headers. The focus is on resolving issues related to data formatting and file writing in MATLAB.

Discussion Character

  • Technical explanation

Main Points Raised

  • The original poster describes their attempt to combine a matrix with headers using the xlswrite function but encounters a dimension mismatch error when trying to concatenate the header and matrix data.
  • Another participant suggests an alternative approach by writing the data to a CSV file instead of using Excel, providing a code snippet that formats the headers and matrix correctly.

Areas of Agreement / Disagreement

There is no consensus on the best method to achieve the desired outcome, as the original poster's method is not working, and an alternative solution is proposed without confirming if it meets the original poster's needs.

Contextual Notes

The original poster's approach using xlswrite may depend on the specific structure of the data and the compatibility of the matrix dimensions with the header format. The alternative solution provided involves writing to a CSV file, which may have different implications for data handling.

Who May Find This Useful

Users looking to export data from MATLAB to Excel or CSV formats, particularly those who are new to MATLAB and seeking practical coding solutions.

swartzism
Messages
103
Reaction score
0
I've discovered that MATLAB is not my forte. I have a 51x3 matrix that I want to print to an Excel sheet, but with column headers. I can get the matrix into an Excel file and I can get the headers into an Excel file, but I cannot figure out how to get the two together. I've looked up how to do this and it is not working for me.

What I have is

Code:
sunAngleMtx(:,1) = results.Timestamp;
sunAngleMtx(:,2) = results.SubTargetID;
sunAngleMtx(:,3) = results.SunAngle;

data = {'Timestamp','SubTargetID','SunAngle'; sunAngleMtx};
xlswrite('sunAngleData.xls', data);

and I'm getting the error
Error using vertcat
CAT arguments dimensions are not consistent.

Error in sunAngleCheck (line 12)
data = {'Timestamp','SubTargetID','SunAngle'; sunAngleMtx};

Any idea what I'm doing wrong. I imagine this is just me being a MATLAB noob.

Thanks in advance.
 
Physics news on Phys.org
Solved.
 
How? Other people might find it useful. :smile:
 
Code:
data = 'Timestamp,SubTargetID,SunAngle\n';
fout = fopen('sunAngleData.csv','wt');
fprintf(fout,data);
fprintf(fout,'%d,%f,%f,%f,%f,%f\n',sunAngleMtx');
fclose(fout);
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K