Copy values from mat file to excel

21 ビュー (過去 30 日間)
GoPoLo83 2012 年 1 月 20 日
Hello, I have some mat files with a lot of values. I want to make a code which search some variables and after to copy an excel. For exemple I have variables as * A = 0.008 * B = 1 1 1 1 1 1 * C = 15 * D = 1.5 1.5 4 2 3 I don't succed to copy these values in excel.
Can I define all these variables as a matrice and then to copy in excel? As exemple, X = [A,B,C,D,....] --> I have a lot of variables that I want to copy ... and after to use xlswrite ? Or it will be a problem because I don't have the same type of variables?
Thank you.

回答 (2 件)

Alex 2012 年 1 月 20 日
Types of variables don't matter for excel.
The issue is more going to be where to put them. Just saying "write variable x followed by y into a spreadsheet" makes no sense to a computer.
If each of your variables are vectors, this will be easiest. You can use xlswrite(filename,A,range) to solve this issue.
A = 0.008
A_len = lenght(a);
B = 1 1 1 1 1 1
B_len = length(B);
C = 15
C_len = length(C);
D = 1.5 1.5 4 2 3
d_len = length(D);
a_str = fprintf('A1:A%i',A_len);
xlswrite('fname', A, a_str);
b_str = fprintf('B1:B%i',B_len);
xlswrite('fname', B, b_str);
c_str = fprintf('C1:C%i',C_len);
xlswrite('fname', C, c_str);
d_str = fprintf('D1:D%i',D_len);
xlswrite('fname', D, d_str);
If your variables are not vectors, then this is harder. You'll have to use ranges like A1:H31. But, you could put each of these onto a different sheet in excel (look at the matlab help for that information)

Image Analyst
Image Analyst 2012 年 1 月 20 日
You can write out the numerical arrays using xlswrite() as you would expect - it's real easy. If you want to write out strings, or a combination of strings and numbers, then it gets trickier. You need to create a cell array where the contents of each cell is either one string or one number (not an array of numbers). Pay attention to the demo in the help where they write out mixed variable types using a cell array.
A common problem is where you try to write out a character array (string) and you get one character per cell in Excel. To solve that you need to put the whole string into a MATLAB cell, and write that out instead of the character array directly. Then the MATLAB character string will end up in one cell in Excel, not a bunch of them.



Community Treasure Hunt

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

Start Hunting!

Translated by