In Matlab I’m using oracles data provider to query an oracle database. I want to fill a matlab matrix or 2D array with the result. I’m a .NET guy but not strong in Matlab. We have analysts who use Matlab but don’t know anything about .NET so we’re sort of stuck. We don’t want to dish out the 2k for the Matlab database toolkit they have so that’s why we’re using .NET to query the database from Matlab.
Any ideas how I can easily convert a datatable from .NET to a matrix or 2D array in matlab? Note that we are using the ODP.NET so the normal ADO.NET GetRows() isn’t there. That’s what I would use if we were using MS’s OLE for oracle but the PC’s are Win 7 64-bit and MS doesn’t support oracle anymore on that platform. You have to use oracles ODP and so that’s what we’re using. Using anything else isn’t an option atm so the solution needs to keep that in mind.
Currently I’m able to query the database, and loop through each row/col but having issues adding to a matrix. Namely because I would need to store both numbers or strings in the matrix and while looping through each column I’m getting the type for conversion but for some reason strcmp() isn’t returning a match even though it looks like it should:
for r = 1:rowCount
for c = 1:colCount
type = tbl.Rows.Item(r).Item(c).GetType().ToString();
val = tbl.Rows.Item(r).Item(c);
if strcmp(type,'System.Decimal') == 1
m(r, c) = tbl.Rows.Item(r).Item(c).ToDouble();
elseif strcmp(type, 'System.String' == 1) || strcmp(type, 'System.DateTime' == 1)
m(r, c) = tbl.Rows.Item(r).Item(c).ToString();
end
m(r, c) = tbl.Rows.Item(r).Item(c).ToString();
end
end
With this in mind, how can I make a matrix or 2D array accept any type of data? A variant if you will. I was doing m = zeros(rowCount, colCount); but this seems to make it expect numbers, which makes sense, but I still would want to hold both numbers and strings and let the users figure out the columns they wish to do the math on.
This line is problematic:
Instead of testing that
typeequals toSystem.String, it checks whether'System.String' == 1is the same string astype.I propose to change it to
Or even better, prefer
switchtoif-elseif, because it saves you code lines:This happens because switch can handle string arguments, and compare them correctly.