Making structure arrays programmtically.

11 Ansichten (letzte 30 Tage)
Mahesh
Mahesh am 2 Jul. 2011
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
  5 Kommentare
Oleg Komarov
Oleg Komarov am 4 Jul. 2011
DO you have it on .txt with headers?
Mahesh
Mahesh am 5 Jul. 2011
No. I have it in .xls with headers.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Oleg Komarov
Oleg Komarov am 2 Jul. 2011
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)];
  2 Kommentare
Mahesh
Mahesh am 6 Jul. 2011
Thanks this is perfect.
Also, What if Column A is made of strings not numbers? Also, how does the second line of your code work? Are you trying to concatenate strings with cells?
Thanks for all the help. I have been trying to understand this concept for a long time.
Oleg Komarov
Oleg Komarov am 6 Jul. 2011
1) Col A strings: you have to pack the column A that you will find in the text variable into a cell and concatenate it to num2cell(data,1)
2) second line: take text (which in my case contains only the a row array of headers {'a','b','c'} and below it attach the columns packed into cells [{'a','b','c'}; {(1:4).';(5:8).';(9:12).'}]

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Robert Cumming
Robert Cumming am 5 Jul. 2011
you can make fields in a structure dynamically, i.e.
yourStruct.(yourVariable) = value
The fields in your structure can be accessed incrementally i.e.
yourStruct.(yourVariable)(index)
etc....

Fangjun Jiang
Fangjun Jiang am 5 Jul. 2011
Yes, the structure array would be perfect for your need. Based on your sample data, you can do.
Dayofmonth = {'Day1'; 'Day2'};
Temperature = [15 ; 25];
Pressure = [101.3; 98];
Temperature=mat2cell(Temperature,ones(size(Temperature)),1);
Pressure=mat2cell(Pressure,ones(size(Pressure)),1);
mystruct=struct('Dayofmonth',Dayofmonth,'Temperature',Temperature,'Pressure',Pressure)
mystruct(1)
mystruct(2)
[mystruct.Temperature]
mystruct =
2x1 struct array with fields:
Dayofmonth
Temperature
Pressure
ans =
Dayofmonth: 'Day1'
Temperature: 15
Pressure: 101.3000
ans =
Dayofmonth: 'Day2'
Temperature: 25
Pressure: 98
  3 Kommentare
Fangjun Jiang
Fangjun Jiang am 5 Jul. 2011
To have meaningful field names like Temperature and Pressure, you have to specify it manually one way or another. You could generate field names programmatically, such as,
a=struct('FirstField',1);
for k=2:3
FieldName=['Field',num2str(k)];
a=setfield(a,FieldName,k);
end
BTW, I modified my original code to use function ones(), instead of repmat().
Fangjun Jiang
Fangjun Jiang am 5 Jul. 2011
If you mean dynamic field name, you could also do.
a=struct('FirstField',1);
for k=2:3
FieldName=['Field',num2str(k)];
a.(FieldName)=k;
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by