How to fill a uitable with data in 1xn cell (n 1x1 structs)?

4 次查看(过去 30 天)
Hello,
I'm a newbie to MatLab and I started a project to get data from an online REST API
Using urlread2 I retrieved the data:
header = http_createHeader('Authorization','Bearer XXXXXXXXXXXXXXXXXX-YYYYYYYYYYYYYYYYY')
url = 'https://api-fxpractice.oanda.com/v1/accounts'
testvar = urlread2 (url,'GET','',header)
so I got in testvar the json data...
testvar:
'{
"accounts" : [
{
"accountId" : 1125870,
"accountName" : "Primary",
"accountCurrency" : "EUR",
"marginRate" : 0.05
},
{
"accountId" : 9244246,
"accountName" : "Bull",
"accountCurrency" : "EUR",
"marginRate" : 0.02
},
{
"accountId" : 6955195,
"accountName" : "Bear",
"accountCurrency" : "EUR",
"marginRate" : 0.02
},
{
"accountId" : 1614502,
"accountName" : "MT4_EUR_Practice",
"accountCurrency" : "EUR",
"marginRate" : 0.05,
"accountPropertyName" : [
"MT4"
]
},
{
"accountId" : 9995814,
"accountName" : "Coaching",
"accountCurrency" : "EUR",
"marginRate" : 0.02
}
]
}'
I want to represent this data in a clickable uitable with one row per each account ( in this example 5 rows) and 4 columns: accountId, accountName, accountCurrency and marginRate.
(when a row has 5 fields (see the 4th account in this example accountID 1614502) the 5th field should be skipped)
First I used parse_json:
test_A = parse_json(testvar)
test_A is a 1x1 cell containing 1x1 struct.
To get rid of the cell I used:
test_B = test_A{1,1}
test_B is a 1x1 struct with 1 field named accounts and containing 1x5 cell (the size of the cell will change depending on the data received)
to get rid of the struct I used:
test_C = struct2cell(test_B)
test_C is a 1x1 cell containing one 1x5 cell, to reveal the 1x5 cell I used:
test_D=test_C{1,1}
test_D is a 1x5 cell containing in this example five 1x1 structs each with 4 or 5 fields
cell 1,1:
accountId: 1125870
accountName: 'Primary'
accountCurrency: 'EUR'
marginRate: 0.0500
cell 1,2:
accountId: 9244246
accountName: 'Bull'
accountCurrency: 'EUR'
marginRate: 0.0200
cell 1,3:
accountId: 6955195
accountName: 'Bear'
accountCurrency: 'EUR'
marginRate: 0.0200
cell 1,4:
accountId: 1614502
accountName: 'MT4_EUR_Practice'
accountCurrency: 'EUR'
marginRate: 0.0200
accountPropertyName: 1x1 cell
cell 1,5:
accountId: 9995814
accountName: 'Coaching'
accountCurrency: 'EUR'
marginRate: 0.0200
now I need to use the first four fields in each struct to fill the uitable and I don't know how to do it... Please can you help me?
Cheers
Francisco

采纳的回答

Matt Cohen
Matt Cohen 2016-5-18
Hi Francisco,
I understand you are interested in filling a uitable with the data stored in a 1xn cell array, where each element of the cell array is a structure.
The documentation for uitable provides some examples that show multiple ways of creating a uitable from data. One of these examples shows that you can pass in data as a cell array into the "uitable" function; however, the values within the cell array must be either numeric, logical, or chars, i.e. it will not accept structures or cell arrays as the elements of the cell array.
To make this work with your current data, you can convert the current 1x5 cell array of structures into a 5x4 cell array of numerical values and chars. Then you can pass this new cell array into the "uitable" function. I have provided some example code that should accomplish this task:
test_E = {};
numRows = 5;
numCols = 4;
rowNames = {'A1','A2','A3','A4','A5'};
colNames = {'accountID','accountName','accountCurrency','marginRate'};
for m = 1:numRows
for n = 1:numCols
test_E{m,n} = test_D{m}.(colNames{n});
end
end
% Create the uitable
t = uitable('Data', test_E,...
'ColumnName', colNames,...
'ColumnEditable', [false false false false],...
'RowName',rowNames);
I have included the 'ColumnEditable' property in the example to show that various other parameters exist and can be played around with to modify the uitable's appearance and properties.
I hope this information proves to be helpful.
Matt
  1 个评论
Francisco
Francisco 2016-5-19
编辑:Francisco 2016-5-19
Thanks a lot for your help!
As the number of rows in the REST API server answer is unknown at design time, I've modified your code accordingly:
header = http_createHeader('Authorization','Bearer XXXXXXXXXXXXXXXXXX-YYYYYYYYYYYYYYYYY');
url = 'https://api-fxpractice.oanda.com/v1/accounts';
testvar = urlread2 (url,'GET','',header);
test_A = parse_json(testvar);
test_B = test_A{1,1};
test_C=struct2cell(test_B);
test_D=test_C{1,1};
test_E = {};
size_test_D = size(test_D);
numRows = size_test_D(1,2);
for n = 1:numRows
test_E{n,1} = struct2cell(test_D{1,n});
end
test_F = {};
numCols = 4;
for m = 1:numRows
for n = 1:numCols
test_F{m,n} = test_E{m,1}{n,1};
end
end
colNames = {'ID','Name','Currency','Margin Rate'};
t = uitable('Data', test_F, 'ColumnName', colNames, 'ColumnEditable', [false false false false]);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by