What concerns your code, it should be only slightly fixed to do what you want. The corrected code is as follows:
while rs.next()
count=count+1;
result(count).(name1)=char(rs.getString(2));
result(count).(name2)=char(rs.getString(3));
...
end
The matter was in dynamic field references, i.e. when you reference some field by its name put in a string variable, this is done by the notation .()
. For more details on dynamic field referencing please see, for instance, a post "Use Dynamic Field References" by Loren Shure on the blog "Loren on the Art of Matlab".
But I'd like to note that, as far as I know, using JDBC as a connector to PostgreSQL is reasonable only for the case
when you have to import/export a rather limited volume of data of only scalar types (scalar numerics, logicals, strings, timestamps and so on). If you have to deal with more complex types (such as arrays) or the volume of data is rather big (about 1Gb), JDBC is no longer efficient (not to say in certain cases almost impossible to use). IMHO for such situations there is a more efficient and convenient way to solve both your problems (retrieving column names and importing data in the form of Matlab structures). Namely, you can use PgMex for these purposes. Your code above can be transformed in such a way (we assume that all the parameters below marked by <>
signs are properly filled and the corresponding table exists in the respective database):
% Create the database connection
dbConn=com.allied.pgmex.pgmexec('connect',[...
'host=<yourhost> dbname=<yourdb> port=<yourport> '...
'user=<your_postgres_username> password=<your_postgres_password>']);
% A test query
sql='select * from <table>'; % Gets all records
pgResult=com.allied.pgmex.pgmexec('exec',dbConn,sql); % Perform this test query
To get column names you need to execute the following code:
nFields=com.allied.pgmex.pgmexec('nFields',pgResult);
fieldNameCVec=cell(nFields,1);
for iField=1:nFields
fieldNameCVec{iField}=com.allied.pgmex.pgmexec('fName',pgResult,iField-1);
end
Finally, to put these results in a structure you need to do as follows:
% Read the results
outCVec=cell(nFields,1);
fieldSpecStr='%<field_type_1> %<field_type_2> ...';
inpCVec=num2cell(0:nFields-1);
[outCVec{:}]=com.allied.pgmex.pgmexec('getf',pgResult,...
fieldSpecStr,inpCVec{:});
% Get only values ignoring NULLs (if NULLS are not to be ignored, the code
% should be improved by taking into accout not only valueVec, but
% also isNullVec and isValueNull being indicators of NULLs)
fieldValCVec=cellfun(@(SFieldValInfo)SFieldValInfo.valueVec,...
outCVec,'UniformOutput',false);
SResult=struct(transpose([fieldNameCVec(:) fieldValCVec(:)]));
What concerns the format of input and output arguments for the command
getf
(including fieldSpecStr
) please see its documentation on PgMex website.
It should be noted, that values of fields for all tuples are retrieved
for one call of the command (instead of a cycle you use in your own code)
and that it is done very fast (approximately 3.5 times
faster rather than the same operation done through Matlab Database Toolbox
working via a direct JDBC connection as mentioned at the end of
"Performance comparison of PostgreSQL connectors in Matlab" article).
And you do not need to convert these values somehow, all is done in Matlab-friendly and native way (in a form of matrices,
multi-dimensional arrays, structures and arbitrary other Matlab formats).
All values of each particular field are put by the code above into
the respective field of SResult structure simply as an array with a size along the first dimension coinciding with the number of tuples
retrieved. If you like to have results for each separate tuple in a separate
structure, you can use the following code:
fieldValCVec=cellfun(@(valueVec)num2cell(valueVec,[2 ndims(valueVec)]),...
fieldValCVec,'UniformOutput',false);
tupleFieldValCMat=transpose(horzcat(fieldValCVec{:}));
SResultCVec=cellfun(@(tupleFieldValCVec)struct(...
transpose([fieldNameCVec(:) tupleFieldValCVec(:)])),...
num2cell(tupleFieldValCMat,1),'UniformOutput',false);
SResultVec=vertcat(SResultCVec{:});
EDIT: Academic licenses for PgMex are free of charge.