MATLAB: How to Write all Variables in workspace into excel

exporting excel

Example
In Workspace Variable stored :
a 23384×1 double
b 122223×1 double
c 23384×1 double
d 23384×1 double
e 'ADDC'
Output Required in Excel
a b c d e
1 1 ADDC
3 1 ADDC
3 2 ADDC
4 4
. 4
. 4
. .
.
. 1222223 row
.
23384 row

Best Answer

The easiest way is probably to put everything into a cell array and then write it to a .xls file using writecell. The question then becomes about the best way to create the cell array. If you have a relatively small number of variables that's always the same and has the same size, you can just do it manually.
If you want to automate, here's one way to do it:
a = [1;3;3;4];
b = [1;1;2;4;4;4];
e = 'ADDC';
save variables % save workspace to variables.mat
data = load('variables'); % load back in and assign to struct variable
f = fieldnames(data); % cell containing variable names
nf = numel(f); % number of variables
sz = zeros(nf,1); % array to hold dimensions of variables
% Here we get variable dimensions for each variable
for j = 1:nf
dataj = data.(f{j}); % load in variable j
% convert char arrays to string
if ischar(dataj)
dataj = convertCharsToStrings(dataj);
data.(f{j}) = dataj;
end
sz(j) = numel(dataj); % size of variable j
end
mxsz = max(sz); % max variable size
c = cell(mxsz+1,nf); % cell array to hold data
c(1,:) = f'; % column headers
for j = 1:nf
dataj = data.(f{j})(:); % variable j (turned into a column vector if necessary)
c(2:sz(j)+1,j) = num2cell(dataj); % assign to cell array
end
The cell array c is:
7×3 cell array
{'a' } {'b'} {'e' }
{[ 1]} {[1]} {["ADDC" ]}
{[ 3]} {[1]} {0×0 double}
{[ 3]} {[2]} {0×0 double}
{[ 4]} {[4]} {0×0 double}
{0×0 double} {[4]} {0×0 double}
{0×0 double} {[4]} {0×0 double}
Note that I'm unclear on what you wanted for that last column. In your post, you put three rows of "ADDC", and then nothing afterward. I'm guessing that's not what you want. If you only want one appearance of "ADDC", then the above is how you do it. If instead you wanted each character in the character array in a different row, take out the "convert char arrays to string" block, in which case c would be:
7×3 cell array
{'a' } {'b'} {'e' }
{[ 1]} {[1]} {'A' }
{[ 3]} {[1]} {'D' }
{[ 3]} {[2]} {'D' }
{[ 4]} {[4]} {'C' }
{0×0 double} {[4]} {0×0 double}
{0×0 double} {[4]} {0×0 double}
In either case, you can write this to an Excel file by
writecell(c,'variables.xls')
NB: DON'T USE a .xlsx EXTENSION HERE. For some reason, empty values don't get written properly in the .xlsx format and you'll get some strange looking output. Use .xls instead, and if you need to convert to .xlsx, do that manually from inside Excel ("Save as").