MATLAB Answers

0

CreateData function in MBC, selecting variables from a .mat file

Hi,
I am attempting to automate an MBC process and have been reviewing the Gasoline DIVCP Project command-line example from the help files. I understand the syntax involved with CreateData and do not receive an error message. However, when I run the script, a window opens in MBC asking me to select the variables I want to use in my dataset from the .mat file I specified in my CreateData function.
My questions are:
1) Why does D = CreateData(project, datafile) not select all the variables from the .mat datafile?
2) How can I use script to select the desired variables from the .mat datafile to put into my dataset?
Thank you for any help you may offer,
Dylan

  0 Comments

Sign in to comment.

2 Answers

Answer by Ian Noell on 9 Jun 2011
 Accepted Answer

Dylan,
The appearance of the dialog was a bug that was fixed for R2009b. Can you please confirm which version of MBC you are using?
Loading data from a MAT file is not supported at the command-line as it is necessary to know what variables from the MAT file should be loaded into MBC. The dialog you saw allowed you to select the variables to load into MBC. However, using a dialog at the command-line is not acceptable as it interrupts the flow of a program.
An alternative is to load the MAT file in your own script and build a structure with the fields required by model.data.ImportFromMBCDataStructure. You can then use ImportFromMBCDataStructure to import data into a model.data object.
Hope that helps,
Ian

  1 Comment

Hi Ian, thanks for your help. Are you saying that this dialog box will not appear on more recent versions of MATLAB? I am running on R2007b. Also, I am not sure how I would convert the .mat file variables into an mbcStruct using script. Would you be able to detail that for me?
Thanks, Dylan

Sign in to comment.


Answer by Richard
on 10 Jun 2011

Following on from Ian's reply, here is a concrete example of importing using a data structure. The easiest and most robust way of ensuring your structure has the right format is to export an empty one and then put data into it:
% Assume "load myData" creates 4 vectors, A,B,C,D
A = rand(100,1);
B = rand(100,1);
C = rand(100,1);
D = rand(100,1);
% Create an empty data object
data = mbcmodel.CreateData;
% Get a correctly formatted structure
S = ExportToMBCDataStructure(data);
% Place data into the structure
S.varNames = {'A', 'B', 'C', 'D'};
S.data = [A(:), B(:), C(:), D(:)];
% Units and comment are optional
S.varUnits = {'km', 'inches', 'fathoms', ''};
S.comment = 'Imported from mat data';
% Import the structure into the data object
data = BeginEdit(data);
data = ImportFromMBCDataStructure(data , S);
data = CommitEdit(data);

  0 Comments

Sign in to comment.