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

4 visualizaciones (últimos 30 días)
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 comentarios
L
L el 16 de Dic. de 2016
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
John BG
John BG el 29 de Dic. de 2016
Editada: John BG el 29 de Dic. de 2016
L
if you store the table in CSV format then both Excel and Access will open it and there you can display and process.
John BG

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 19 de Dic. de 2016
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 comentarios
L
L el 20 de En. de 2017
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 el 27 de En. de 2017
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!

Iniciar sesión para comentar.

Más respuestas (1)

John BG
John BG el 16 de Dic. de 2016
Editada: John BG el 29 de Dic. de 2016
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
  2 comentarios
John BG
John BG el 29 de Dic. de 2016
L
if you store the table in CSV format then both Excel and Access will open it and there you can display and process.
John BG

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by