How to specify the filename using a string when exporting from matlab
7 次查看(过去 30 天)
显示 更早的评论
I am exporting cell arrays to excel using xlswrite and that works fine. But I want the exported file name to be from a string I specify from the file I am using in excel. I am using the following code:
strings = strings(1:1,1);
xlswrite('strings',horzcat);
If i open the 'strings' link matlab knows its the cell I want (e.g. 250nm) but when the file is created it is called strings rather than the cell I specified. Any help would be greatly appreciated.
3 个评论
Image Analyst
2016-2-3
It's not supposed to be a function. The second argument to xlswrite() is the data variable, not a function.
回答(2 个)
Star Strider
2016-2-2
The problem is likely your putting single quotes around 'strings', creating it as a string variable. See if removing the single quotes solves the problem:
xlswrite(strings,horzcat);
This should pass the variable ‘strings’ to the function.
0 个评论
Image Analyst
2016-2-3
Like you said, exporting cell arrays is fine. But exporting strings is a problem. If you export a string you'll get one character per cell in Excel, which is usually not what you want. To prevent that you need to encapsulate your string in a cell.
By the way, this code
strings = strings(1:1,1);
xlswrite('strings',horzcat);
is very wrong in so many ways. Anyway, let's assume strings is a cell array of a bunch of strings of different lengths. Let's say you want the third string from that cell array. To get that string you'd do
string3 = strings{3}; % Note, use {} to get contents, not ().
Now if you do
xlswrite(xlFileName, string3, 'A1');
you'll get one character per cell like I said before. So you can either not extract the contents and just send the cell itself:
xlswrite(xlFileName, strings(3), 'A1');
OR you can take string3 and enclose it in {} which will turn it back into a cell
ca = {string3}; % Put string3 inside a cell called ca.
xlswrite(xlFileName, ca, 'A1'); % Export cell array ca to Excel.
Either of those two methods I just showed will export a cell which has a string inside it and the string will appear in a single cell in Excel, not strung across multiple cells.
Also, please remember to read the FAQ http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F which I think will give you a better intuitive feel for how to deal with cells.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!