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

조회 수: 10 (최근 30일)
Francisco
Francisco 2016년 5월 16일
편집: Francisco 2016년 5월 19일
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에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by