How can I create a table in Access with Matlab that allows for different names of columns and rows without being hard coded?

4 次查看(过去 30 天)
Hello,
So I'm making progress in my creation of a function that creates a table in Access from Matlab, but I'm trying to make it even more robust. What I mean by this is that, lets say I have an n by m (where n can be any number of rows and m can be any number of columns) cell (imported as data from excel and turn into said cell) and I want it to be able to pick up the first row and first column of this cell as the titles to the data the is inside the n by m cell. The row and column names can change so it has to be able to detect it from the cell, I just don't know how to do that or I am close but not doing it right.
Here's what I've got so far.
CreateTable = ['CREATE TABLE Item(Item ID varchar, '...
'Item Number varchar,Location varchar,Order int)'];
I also don't think this is complete because I believe this only does the first column of the data and misses the top first row with other indicators.
I'm think there has got to be a way to get it to where it would be something like:
['CREATE TABLE Item(data(1,1:end) varchar)'];
Where data is the n by m cell. Again, still working on how to grab the names given in the top row.
Is this possible?
Thank you,
L
  2 个评论
L
L 2016-12-16
So I've figured out half (or maybe a quarter of the problem) of it; I put together these two lines.
ColumnNames = data(1,:); % For the names I want to put in the first row of the new table in Access.
RowNames = data(2:end,1); % For the other names I need in the table that start below data(1) and proceed down the first column.
Now I just need to figure out how to export these two things into the Create Table part and I should be all set. Problem is, I'm struggling with the syntax to pull that off. Assuming it can even be done.
I appreciate everyone's help.
Thank you,
L

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2016-12-19
Note that you can link Access directly to an Excel file. There's no need to go through matlab through that. Maybe you should explore that first.
I find your question odd and it doesn't look you fully understand how databases work. You can't name rows of a database, it's simply not even a concept. As for naming columns, that is exactly what you do in your CREATE TABLE statement, you give a name to each column. In your example these are 'Item Id', 'Item number', 'Location', and 'Order'. So you already know how to name these.
If you want to be able to build your CREATE TABLE dynamically from entries in a cell array, then you're going to have to do something to what I showed in your previous question. In your statement, you not only need to dynamically put the names of the row, that's easy to do, but also the types of the rows, that's a lot more complicated if you need to infer that from the cell array itself.
One very rough solution, untested (there may be bugs / typos):
function sql = maketablefromcell(tablename, c)
%tablename: the name of the table to create
%c: cell array, whose first row contains the names of the columns
%The type of the columns is infered from the content of rows 2 onward and is either integer, varchar, double or boolean
colsql = cell(1, size(c, 2));
for col = 1:size(c, 2)
if ischar(c{2, col})
coltype = 'varchar'; %assume all other rows are char
elseif islogical(c{2, col})
coltype = 'bit'; %assume all other rows are logical. BIT is SQL data type for logical
elseif isnumeric(c{2, col})
allnums = cell2mat(cellfun(@(m) m(:), c, 'UniformOutput', false)); %reshape row 2:end to column vector and concatenate into one matrix
if all(mod(allnums, 1) == 0)
coltype = 'integer';
else
coltype = 'float'; %SQL float is matlab double
end
else
error('Unrecognised data type for column %d', col);
end
colsql{col} = sprintf('%s %s', c{1, col}, coltype);
end
sql = sprintf('CREATE TABLE %s (%s)', tablename, strjoin(colsql, ','));
end
  15 个评论
L
L 2017-1-20
Ok great. Thank you, I figured it was something like that but messing with it I couldn't get it to stick. I supposed it is similar to modifying the SQL statement in one of the above posts. So columns should equal something like (and I'm guessing right now) sprintf{'[%s]',strjoin(tabledata,',')}. Along those lines, I think. Thank you for your continued support Guillaume, I apologize for my ignorance.
L
L 2017-1-27
Thank you for all of your help Guillaume. I've figured it all out. I just had to add the [] to the column names before I handed it off to the datainsert function. Everything works.
Thank you again!

请先登录,再进行评论。

更多回答(1 个)

John BG
John BG 2016-12-16
编辑:John BG 2016-12-29
Why not starting from any of this links
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help, please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

产品

Community Treasure Hunt

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

Start Hunting!

Translated by