How to specify the filename using a string when exporting from matlab

4 views (last 30 days)
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
Alun Owen
Alun Owen on 3 Feb 2016
But if I don't name it "horzcat" how would matlab identify it as a function and then when I use xlswrite I need to use horzcat so matlab can identify what it is exporting.
Image Analyst
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.

Sign in to comment.

Answers (2)

Star Strider
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.

Image Analyst
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.
  1 Comment
Alun Owen
Alun Owen on 3 Feb 2016
Hi Image Analyst, My apologies I have obviously worded my question incorrectly. I don't need the exported string in an excel sheet I'm trying to use the string to become the new filename once I export from Matlab to excel. Thanks Alun

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!