user42459
user42459

Reputation: 915

xlswrite: Showing variable name in sheet name or somewhere

  1. I am using xlswrite.

I am putting lots of variables into different sheets of one xlsx file.

One variable for one sheet.

So I want to put the name of the variable as the name of the sheet.

I couldn't find any command which does this. So perhaps this should be done by coding some python and having one line in matlab which calls that py file.

  1. If this is impossible, I will just try to put the name of the variable in the first row. However, when the variable has more than 1 column, then it says vertcat doesn't work.

    xlswrite('test.xlsx',vertcat('A', [1 3; 1 2]))

What should I do?

======================

A follow-up question

I coded the following based on Suever's answer.

function xlswr(file_name,varargin)
xlswrite(file_name,varargin{i},inputname(i+1))

However, when I call this function by

xlswr('test.xlsx', A, A(:,2))

then A is printed on test.xlsx, but A(:,2) was not.

Whenever the input is not just whole name like A, but some part of the variable, then it doesn't print it out.

How can I fix this?

Upvotes: 0

Views: 241

Answers (1)

Suever
Suever

Reputation: 65430

The reason for your issue is that you're attempting to vertically concatenate a string that is 1 x 1 with data that is 2 x 2 so you obviously are going to have a dimension mis-match.

If you want to specify the sheet name, then simply use the third input to xlswrite to specify the name of the sheet as a string.

xlswrite('test.xlsx', [1 3; 1 2], 'A')

Alternately, if you want to include text and numeric data, you can always pass a cell array rather than a numeric array. This will place strings and numbers into the same sheet.

xlswrite('test.xlsx', vertcat({'A', 'B'}, num2cell([1 3; 1 2])));

Upvotes: 1

Related Questions