MATLAB: Making structure arrays programmtically.

MATLABstruct2cellstructure arrays

Hello Guys,
I have been trying to do the following programmatically and I have not been successful so far (excuse my ignorance if this is a trivial problem for most of you).
I have the following data:
Day of Month Temperature Pressure Day1 15 101.3 Day2 25 98
I want a structure, for example, mystruct to look like below.
mystruct.Dayofmonth = {'Day1'; 'Day2'} mystruct.Temperature = [15 ; 25]; mystruct.Pressure = [101.3; 98];
My aim is to group all the temperatures under 'temperature' field and pressures under 'pressure' (and similarly the day of the month field also). I would like to do this programatically so that I can use this concept for creating such structures dynamically(i.e., without having to hard code the name of the fields).
This is my first post under mathworks answers. I have been a great fan of the products and of the openmindedness to share ideas in this group.
Thanks. -Mahesh

Best Answer

If you have the stats TB you can import a file directly as a dataset object.
It is very convenient and easy to use.
EDIT
I have created the following xlsx file:
A B C
1 5 9
2 6 10
3 7 11
4 8 12
Now to create programmatically the structure (independently on the number of columns):
[data,text] = xlsread('test.xlsx');
In = [text;num2cell(data,1)];
S = struct(In{:});
Or if you want a non-scalar structure create first In as:
In = [text;num2cell(num2cell(data),1)];
Related Question