MATLAB Simple MATLAB to Excel Question

  • Thread starter Thread starter swartzism
  • Start date Start date
  • Tags Tags
    Excel Matlab
AI Thread Summary
The discussion revolves around a user struggling to combine a 51x3 matrix with column headers for export to an Excel sheet using MATLAB. The user initially attempts to create a combined data structure but encounters a dimension mismatch error when trying to concatenate the headers with the matrix. After seeking help, a solution is provided that involves writing the data to a CSV file instead. The solution includes creating a header string and using the `fprintf` function to format and write the matrix data correctly, ultimately resolving the issue of exporting the matrix with headers. This approach offers a workaround for users facing similar challenges in MATLAB.
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
Views
6K
Replies
4
Views
6K
Replies
2
Views
2K
Replies
5
Views
8K
Replies
2
Views
4K
Replies
1
Views
13K
Replies
1
Views
4K
Replies
1
Views
12K
Replies
8
Views
2K
Back
Top