问题描述:

let's say I have a table calld temp returned from sql server

that looks like

date column a column b column c

.

.

.

names = fieldnames(temp);

names = names(2:end);

cells = struct2cell(temp);

orgTS = cell2mat(cells(2:end)');

effectiveDate = temp.date

Can anyone explain what the above is doing? It's hard to test because sql server is down and I can't actually run this

网友答案:

MATLAB structs have fields, the names usually accessed with a ., and that's what fieldnames gets you:

fieldnames Get structure field names or object properties.

NAMES = fieldnames(S) returns a cell array of strings containing the names of the fields in structure S.

Your names variable will be a cell of strings, as stated above. Your code gets fields 2:end, so that looks like {'column a','column b','column c'}. The line with cell2mat(cells(2:end)'); is getting the data under these headings.

网友答案:

You may be interested in building a fake database until the server is up. Here is some code and its associated results.

You can evaluate the content of each variable in the command window by typing the name of the variable, in the workspace browser by double clicking on each variable , or in the editor window by pointing your mouse on each variable (you have to set preferences, editor, display, enable datatip in edit mode)

%fake database
field1 = 'date';  value1 = datestr(repmat(now, 5,1));
field2 = 'cola';  value2 = rand(5,1);
field3 = 'colb';  value3 = zeros(5,1);
field4 = 'colc';  value4 = ones(5,1);

%create a temp structure
temp = struct(field1,value1,field2,value2,field3,value3,field4,value4);

The following code

names               = fieldnames(temp);
names               = names(2:end);

gives:

names =

'cola'
'colb'
'colc'

cells               = struct2cell(temp);    
orgTS               = cell2mat(cells(2:end)');

gives

orgTS =

0.7866         0    1.0000
0.5043         0    1.0000
0.4850         0    1.0000
0.8388         0    1.0000
0.0859         0    1.0000

effectiveDate       = temp.date

gives

effectiveDate =

04-Oct-2013 00:24:38
04-Oct-2013 00:24:38
04-Oct-2013 00:24:38
04-Oct-2013 00:24:38
04-Oct-2013 00:24:38
相关阅读:
Top