Sorry... the table did not come through correctly. its the table of Dayofmonth, Temperature and Pressure as columns.
Making structure arrays programmtically.
11 ビュー (過去 30 日間)
古いコメントを表示
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
2011 年 7 月 2 日
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 件のコメント
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
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....
0 件のコメント
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
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
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 Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!