How to specify the filename using a string when exporting from matlab
4 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 3 Feb 2016
It's not supposed to be a function. The second argument to xlswrite() is the data variable, not a function.
Answers (2)
Star Strider
on 2 Feb 2016
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 Comments
Image Analyst
on 3 Feb 2016
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.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!