Question

I'm new at programming, and I've been banging my head trying to figure this out for work.

I am trying to pull roughly 300 mysql tables into Matlab into my workspace.

I have attached the following code, which is designed to pull one table (I plan to loop this code though the 300 mysql tables when it is working).

The code successfully works to import single table into the workspace as a new dataset.

My problem arise when I try to rename this new dataset with the name of the original mysql table.

Please see code below for this part where I screw up (%Assign data to output variable)

I have a list of all the 300 tables names, and I plan to store them in a list called 'name'... Hence, name(1)... is this the right approach?

For example, the original mysql table was called 'options_20020208'.

After I run the script, I need the new dataset that Matlab imports to be called 'options_20020208' as well.

Any ideas here?

%Define Query

 name = 'options_20020208'




%Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'dataset');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');


%Make connection to database.  
conn = database('', 'root', 'password', 'Vendor', 'MYSQL', 'Server', 'localhost', 'PortNumber', 3306);


%Read data from database.
curs = exec(conn, [['SELECT  ',name,'.UnderlyingSymbol , ']...
 ,  [name,'.UnderlyingPrice  , ']...
 ,  [name,'.Expiration  , ']...
 ,  ['FROM ','PriceMatrix.',name,'   ']... 
]);

curs = fetch(curs);

close(curs);

%Assign data to output variable
name(1) = curs.Data;

%Close database connection.
close(conn);

%Clear variables
clear curs conn
Était-ce utile?

La solution

If you have defined a variable name, name(1) means "the first element of variable name" (in this case just "o"). Regardless of the dimensions of the variable it returns a single value (i.e. even if X is some 5-D monstrosity, X(50) returns only the value of the 50th element). name(1) = data means "set the first element of variable name to be equal to data" and will cause an error if data is not of the right size, and either an error or unexpected behaviour if it's not the right type.

For example, try this at the command line:

name = 'options_20020208';
name(1) = 1

Now, technically what you want can be done, although I don't recommend it. If you have all the names in some sort of 300 x (length) variable then over a loop of n = 1:300 it would be something like this (where name is your list of variable names):

eval([name(n,:) ' = curs.Data;'])

This will return 300 variables named 'options_20020208' or similar each containing one set of curs.Data. However, there are better ways of storing data in your workspace that will make further operations on your data easier, for example you could use structures:

myStruct(n).name = name(n,:);
myStruct(n).Data = curs.Data;

If you wanted to do some analysis and then save out all this data in some format, for example, it's going to be much easier to loop over the structure, and set the filename to [myStruct(n).name,'.csv'] and the file contents to mystruct(n).AdjustedData, etc., then to deal with 300 named variables.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top