How do I obtain the column names of a table along with the data when using the Database Toolbox in MATLAB 7.6 (R2008a) ?

12 views (last 30 days)
I am using the Visual query builder (QUERYBUILDER) GUI in MATLAB 7.6 (R2008a) to obtain data from my SQL database. I would like to be able to obtain the data along with a header row populated with the column name.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to obtain data with a header row populated by the field names is not available with the QUERYBUILDER GUI in MATLAB 7.6 (R2008a).
As a workaround, the COLUMNNAMES function can be used to obtain the column names from the table. These values can then be concatenated to the data read in. The following code can be added to the line after
e = fetch(e);
in the code auto generated by the QUERYBUILDER GUI.
%Obtain the raw data
rawData=e.Data;
% rawData =
%
% [1x21 char] [23]
% [1x21 char] [34]
% [1x21 char] [45]
%Obtain the column names as a vector
rawColumnNames=columnnames(e);
% rawColumnNames =
%
% 'date','num'
%Remove the single-quotes
rem = regexprep(rawColumnNames,'''','');
% rem =
%
% date,num
%Parse the single string and create a cell array with column names
i=1;
while ~isempty(rem)
% returns the next word in the string
[colNames{i} rem] = strtok(rem,', ');
i = i+1;
end
%Concatenate the data and the header:
data=[colNames;rawData];
% Final data with the header:
% data =
%
% 'date' 'num'
% [1x21 char] [ 23]
% [1x21 char] [ 34]
% [1x21 char] [ 45]

More Answers (0)

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!