Simple MATLAB to Excel Question

  • MATLAB
  • Thread starter swartzism
  • Start date
  • Tags
    Excel Matlab
In summary, the speaker is having trouble combining a 51x3 matrix and column headers into an Excel sheet using MATLAB. They have attempted to use the xlswrite function, but are receiving an error message. They have found a solution by writing the data and matrix separately to a CSV file.
  • #1
swartzism
103
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
  • #2
Solved.
 
  • #3
How? Other people might find it useful. :smile:
 
  • #4
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);
 
  • #5


Hello,

It seems like the issue may be with the dimensions of your matrix and the data you are trying to concatenate. The error message is indicating that the dimensions are not consistent, meaning they do not match up.

One thing you can try is to transpose your matrix, as shown below, before concatenating it with the headers. This will ensure that the dimensions are consistent.

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

% Transpose the matrix
sunAngleMtx = sunAngleMtx';

% Concatenate the headers and matrix
data = {'Timestamp','SubTargetID','SunAngle'; sunAngleMtx};
xlswrite('sunAngleData.xls', data);

I hope this helps and good luck with your MATLAB and Excel integration!
 

1. How do I export data from MATLAB to Excel?

To export data from MATLAB to Excel, you can use the built-in "xlswrite" function. This function takes in the data and the file name as inputs and creates an Excel file with the data.

2. Can I transfer my plots from MATLAB to Excel?

Yes, you can transfer your plots from MATLAB to Excel by first saving them as image files using the "saveas" function, and then inserting them into an Excel file. Alternatively, you can use the "export_fig" function to directly save the plot in a specified Excel file.

3. How can I import data from Excel to MATLAB?

To import data from Excel to MATLAB, you can use the "xlsread" function. This function takes in the file name and sheet name as inputs and returns the data as a matrix.

4. Is it possible to automate the process of transferring data between MATLAB and Excel?

Yes, you can automate the process of transferring data between MATLAB and Excel by writing a MATLAB script that uses the "xlswrite" and "xlsread" functions. This script can be run whenever you need to transfer data between the two programs.

5. Can I use MATLAB functions in Excel?

No, MATLAB functions cannot be directly used in Excel. However, you can use MATLAB's "COM" or "NET" interface to call MATLAB functions from within Excel. This requires some programming knowledge and may not be suitable for all users.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
8K
  • Other Physics Topics
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
13K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top