Consolidating cells to an xlsx file

I have data in this format
Variables A...Z
A = 1x408 cell
B = 1x408 cell
C = 1x408 cell
.
.
Z = 1x408 cell
Each element in the 1 X 408 cell of A-Z variables is a 1X1 cell.
I want to consolidate the data and generate an excel file that looks like this
Column names: A B C D E F .......... Z
Data values: A1 B1 C1 D1 ........................Z1
A2 B2 C2 D2 ........................Z2
.
.
.
A408 ..............................................Z408
Appreciate your inputs.
Thanks
Sai

3 Comments

"I have data in this format Variables A...Z"
Having lots of variables like that makes processing data harder.
So far you have not told us the mot important information: how did you get all of those variables into the workspace? For example, if they are LOADed from a MAT file, then you should always LOAD into an output variable:
S = load(..)
I got these variables in a .mat file
"I got these variables in a .mat file"
To avoid the very bad situation that you are in, always LOAD MAT files into an output variable:
S = load(..)
Then process the content of the structure S. You will probably find many useful MATLAB functions to help you, e.g. FIELDNAMES, STRUCT2CELL, STRUCT2TABLE, dynamic fieldnames:
and also comma-separated lists:
"I have data in this format Variables A...Z"
Trying to process lots and lots of separate variables will force you into writing slow, complex, inefficient code. Best avoided.

Sign in to comment.

Answers (1)

dpb
dpb on 11 Apr 2023
Moved: dpb on 11 Apr 2023
"I have data in this format"
A = 1x408 cell
B = 1x408 cell
C = 1x408 cell
That's your problem -- don't do that.
Create a single array of 26x408 to begin with; the result otherwise is, as you're discovering, very difficult to deal with effectively. We won't even talk about the slow, inefficient and difficult to debug alternatives -- "There be dragons...."

4 Comments

Appreciate your views but, I have not generated the data in this format. This is the output I have got from an existing tool. I am trying to convert it to a format that I need. If you have a solution please let me know.
Thanks.
dpb
dpb on 11 Apr 2023
Edited: dpb on 11 Apr 2023
Yeech! if that is really the case.
What is the original form of the data in when you get it? Maybe we can fix something earlier...is it already a MATLAB .mat file or another spreadsheet or a collection of files or...????
Otherwise, presuming you do have fixed number of variables to deal with, while it's ugly and awkward, the brute-force answer is
m=cell2mat(cat(1,A{:},B{:},C{:}, ..., X{:},Y{:},Z{:})).';
I got these variables in a .mat file.
What does
whos -file yourmatfilename.mat
return? Are these the only variables in the file or do you also have to find out "who's who in the zoo" in order to process the correct variables (and only the correct variables)?
As @Stephen23 points out, load-ing the .mat file into a struct variable then provides much more flexibility in writing code, but you still need to know something about what you're writing code for. But, that does open it up to using dynamic field names.
How to approach it depends in part on just what is in the file and if it can ever change its content--if it is immutable and unless it matches the contract to contain those specific variables and all of them and is nothing if that contract isn't met, then the single hardcoded line as given above may be a viable solution.

Sign in to comment.

Categories

Commented:

dpb
on 12 Apr 2023

Community Treasure Hunt

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

Start Hunting!