Making structure arrays programmtically.

조회 수: 9 (최근 30일)
Mahesh
Mahesh 2011년 7월 2일
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
Oleg Komarov
Oleg Komarov 2011년 7월 4일
DO you have it on .txt with headers?
Mahesh
Mahesh 2011년 7월 5일
No. I have it in .xls with headers.

댓글을 달려면 로그인하십시오.

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 2일
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
Mahesh
Mahesh 2011년 7월 6일
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 2011년 7월 6일
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).'}]

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Robert Cumming
Robert Cumming 2011년 7월 5일
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 2011년 7월 5일
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
Fangjun Jiang
Fangjun Jiang 2011년 7월 5일
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 2011년 7월 5일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by