📄 matlabimport.h
字号:
/*------------------------------------------------------------------------------*
* File Name: MatlabImport.h *
* Creation: Soapy 8/10/2003 QA70-4977 *
* Purpose: Matlab Import Dialog Class *
* Copyright (c) Originlab Corporation, 2002, 2003 *
* All Rights Reserved *
* *
* Modification Log: *
* *
* Soapy 8/12/03 TEMP_SOLUTION_OTHER_DATA_TYPE_SUPPORT_BY_DOUBLE_COPYING *
* DVT 8/14/03 MATLAB_SINGLE_INSTANCE_FOR_MENU_IMPORT *
*------------------------------------------------------------------------------*/
#include <Dialog.h>
#include "ResizeDialog.h"
#include <externApps.h> // matlab class
#include "ODlg.h" // resource IDs, ODlg.dll also use this file for its res ids
#include "HelpID.h" // dialog ID
#include "MATVarList.h" // MATVarList class
#define DEBUG_RETURN //return TRUE;
#define _DBINT(_STR, _INT) //out_int(_STR, _INT);
#define _DBSTR(_STR) //out_str(_STR);
#define STR_NO_MATLAB "Matlab not available"
#define STR_TEMP_WORKSPACE_NAME "_TMP_Origin_Import_Save_.mat"
#define STR_TEMP_ARRAY_NAME "TMP_Var__For_Type_Transfer"
#define MATLAB_OUTPUT_NUMTOKENS 4
#define MATLAB_OUTPUT_SKIP_LINES 2
// Type of MATLAB variables
enum {
MATLAB_VAR_DOUBLE_ARRAY,
MATLAB_VAR_COMPLEX_ARRAY,
MATLAB_VAR_CHAR_ARRAY,
MATLAB_VAR_INTEGER_ARRAY,
MATLAB_VAR_SINGLE_ARRAY,
MATLAB_VAR_UNKNOWN
};
#define MATLAB_OUTPUT_NAME_COL 0
#define MATLAB_OUTPUT_TYPE_COL 3
#define MATLAB_WKS_NAME "Matlab"
/*
This class is for the import dialog of MATLAB variables from MAT file
*/
class MatlabImportDlg : public ResizeDialog
{
public:
MatlabImportDlg() : ResizeDialog(IDD_MATLAB_IMPORT, "ODlg")
{
}
public:
int DoModalEx(HWND hWndParent = NULL, LPCSTR lpcszMatlabWkspaceName = NULL, BOOL bSingle = FALSE)
{
InitMsgMap();// will be called from internal later
m_strMatlabWorkspaceFilename = lpcszMatlabWkspaceName; // the MAT file name
m_bSingle = bSingle; /// DVT should be connected to m_bRestoreWorkspace
int nRet = ResizeDialog::DoModal(hWndParent);
return nRet;
}
protected:
EVENTS_BEGIN
ON_INIT(OnInitDialog)
ON_SIZE(OnDlgResize)
ON_OK(OnClose)
ON_TIMER(OnTimer)
ON_DESTROY(OnDestroy)
ON_BN_CLICKED(IDC_MATLAB_BTN_IMPORT, OnImportAsMatrices)
ON_BN_CLICKED(IDC_MATLAB_BTN_IMPORT_WKS, OnImportAsSingleWks)
ON_BN_CLICKED(IDC_MATLAB_CHK_REPLACE_MAT, OnClickReplaceMat)
ON_BN_CLICKED(IDC_MATLAB_CHK_REPLACE_WKS, OnClickReplaceCol)
ON_GRID_SEL_CHANGE(IDC_MATLAB_GRID, OnVsFlexSelChange)
ON_BN_CLICKED(IDC_MATLAB_BTN_IMPORT_UPDATE, OnUpdateWorkSpace)
EVENTS_END
//ON_BN_CLICKED(IDC_MATLAB_KEEP_RUNNING, OnCheckKeepRunning)
protected:
///DVT - IDOK in error is in dialog resource instead of IDCANCEL - change
BOOL OnClose()
{
return TRUE;
}
void OnVsFlexSelChange(Control ctrl)
{
if ( m_bDisableImportBtnUpdate )
return;
BOOL bHasSelection = 0 < m_vsFlex.SelectedRows();
//DVT: should actualy disable/enable based on importability.
//this is not complete version, should actualy de-select MATLAB_VAR_UNKNOWN rows
BOOL bHasWksSelection = FALSE;
BOOL bHasMatSelection = FALSE;
if( bHasSelection )
{
int nRowsSelected = m_vsFlex.SelectedRows();
for( int ii=0; ii < nRowsSelected; ii++ )
{
long nR = m_vsFlex.SelectedRow(ii);
int nVarType = GetMatlabVarType(nR);
if ((nVarType == MATLAB_VAR_DOUBLE_ARRAY )||
(nVarType == MATLAB_VAR_INTEGER_ARRAY)||
(nVarType == MATLAB_VAR_SINGLE_ARRAY))
{
bHasWksSelection = TRUE;
bHasMatSelection = TRUE;
break;
}
if (nVarType == MATLAB_VAR_COMPLEX_ARRAY )
{
bHasMatSelection = TRUE;
}
}
}
EnableImportWksBtn(bHasWksSelection);
EnableImportMatBtn(bHasMatSelection);
}
// Enable the ImportAsWorksheet Button
void EnableImportWksBtn(bool bEnable = true)
{
m_btnImportWks.Enable = bEnable;
}
// Enable the ImportAsMatrix Button
void EnableImportMatBtn(bool bEnable = true)
{
m_btnImport.Enable = bEnable;
}
// Initialize the MatlabImport Dialog
BOOL OnInitDialog()
{
DEBUG_RETURN
ResizeDialog::OnInitDialog();
GetWindow().Text = "Connecting to Matlab...";
m_bRestoreWorkspace = false;
if(!m_strMatlabWorkspaceFilename.IsEmpty()) // file is specified
{
if(m_mlObj && !m_bSingle)
{
// save workspce and when finish, restore wkspace
if(SaveCurrentWorkspace())
m_bRestoreWorkspace = true;
}
}
m_bDisableImportBtnUpdate = true;
// The Variable List
m_vsFlex.Init(IDC_MATLAB_GRID, *this);
m_vsFlex.ShowTheGrid(FALSE); // hide the grid until attach to MATLAB
m_nTimerID = SetTimer(5432,300);
// ImportAsMatrix Button
m_btnImport = GetItem(IDC_MATLAB_BTN_IMPORT);
EnableImportMatBtn(false);
// ImportAsWorksheet Button
m_btnImportWks = GetItem(IDC_MATLAB_BTN_IMPORT_WKS);
EnableImportWksBtn(false);
// The Update Button
m_btnUpdate = GetItem(IDC_MATLAB_BTN_IMPORT_UPDATE);
//Keep Matlab alive check box
/*
m_chkKeepRunning = GetItem(IDC_MATLAB_KEEP_RUNNING);
// CPY make invisible until we have better support
m_chkKeepRunning.Visible = false;//!m_bSingle;
m_bKeepMatlabAlive = m_mlObj ? m_mlObj.KeepMatlabRunning: FALSE;
m_chkKeepRunning.Check = m_bKeepMatlabAlive;
*/
// The two check boxes
Control ctrlReplaceMat = GetItem(IDC_MATLAB_CHK_REPLACE_MAT);
Control ctrlReplaceWks = GetItem(IDC_MATLAB_CHK_REPLACE_WKS);
ctrlReplaceMat.Value = 1;
ctrlReplaceWks.Value = 1;
m_bUpdateMatrix = true;
m_bUpdateCols = true;
return TRUE;
}
BOOL OnTimer(UINT nTimerID)
{
ASSERT(nTimerID==m_nTimerID);
KillTimer(m_nTimerID);
m_nTimerID = 0;
//not good loose connection with static in Console!!! if(m_mlObj.Attach( FALSE, m_bSingle ))
if(m_mlObj.Attach( TRUE, m_bSingle ))
{
//if ( m_bSingle )
// m_mlObj.Visible = FALSE;
//not good, it hides all Matlab sessions!
GetWindow().Text = "Matlab Import";
if( !m_strMatlabWorkspaceFilename.IsEmpty() )
LoadWorkspace(m_strMatlabWorkspaceFilename);
FillTheGridWithData();
m_vsFlex.ShowTheGrid( );
m_bDisableImportBtnUpdate = false;
}
else
GetWindow().Text = STR_NO_MATLAB;
return TRUE;
}
BOOL OnDestroy(void)
{
DEBUG_RETURN
if(!m_strMatlabWorkspaceFilename.IsEmpty()) // file is specified
{
ASSERT(m_mlObj);
if(m_bRestoreWorkspace)
RestoreWorkspace();
else
m_mlObj.Detach();
}
return TRUE;
}
BOOL OnDlgResize(int nType, int cx, int cy)
{
DEBUG_RETURN
uint nCntrlsRight[] = {
IDC_MATLAB_BTN_IMPORT,
IDC_MATLAB_CHK_REPLACE_MAT,
IDC_MATLAB_BTN_IMPORT_WKS,
IDC_MATLAB_CHK_REPLACE_WKS,
IDC_MATLAB_BTN_IMPORT_UPDATE,
//IDC_MATLAB_KEEP_RUNNING,//CP
IDOK,
0};
uint nCntrlsBottom[] = {0};
ResizeMoveControlsRightBottom(IDC_MATLAB_GRID, nCntrlsRight, nCntrlsBottom, cx, cy);
return TRUE;
}
bool SaveCurrentWorkspace(bool bCleanup = true)
{
if(!m_mlObj)
{
return false;
}
string strExecute;
strExecute.Format("save('%s','*');"
"clear all;", STR_TEMP_WORKSPACE_NAME );
string strRet = m_mlObj.Execute(strExecute);
return strRet.IsEmpty(); //success
}
bool RestoreWorkspace()
{
if(!m_mlObj)
{
return false;
}
string strExecute;
strExecute.Format( "clear all;"
"load('%s', '*');"
"delete('%s');", STR_TEMP_WORKSPACE_NAME, STR_TEMP_WORKSPACE_NAME );
string strRet = m_mlObj.Execute(strExecute);
return strRet.IsEmpty(); //success
}
bool LoadWorkspace(LPCSTR lpcstrMatFileName)
{
string strExecute;
strExecute.Format(
"load('%s', '*');",
lpcstrMatFileName );
string strRet = m_mlObj.Execute(strExecute);
return strRet.IsEmpty(); //success
}
////////////////////////////////////////////////////////////////
//this three functions are the CPY code from MatlabConsole.c
BOOL GetMatlabRealMatrices( StringArray &asNames, string& strResult )
{
//bool bReplaceCurrent = m_bUpdateMatrix;
for(int ii = 0; ii < asNames.GetSize(); ii++)
{
bool bNew = false;
MatrixLayer mlay(asNames[ii]);
if(!mlay || (!m_bUpdateMatrix && mlay))
{
if(mlay) // already existed
mlay.Detach();
mlay.Create();
mlay.GetPage().Rename(asNames[ii]);
bNew = true;
}
Matrix<double> mm(mlay);
if(!m_mlObj.GetMatrix(asNames[ii], &mm) && bNew)
{
mlay.Destroy();
strResult += asNames[ii] + " ";
}
}
if(!strResult.IsEmpty())
{
strResult += ": matrices not found in Matlab!";
return FALSE;
}
return TRUE;
}
//--------- Soapy 8/12/03 TEMP_SOLUTION_OTHER_DATA_TYPE_SUPPORT_BY_DOUBLE_COPYING
//use Matlab command double() to cast integer array (int8, int16, int32, uint8, uint16, uint32) and single array to
//double before getting their value to Origin. This is to make things to work temporarily for Matlab Import Dialog until
//related functions are added to Matlab Class.
BOOL GetMatlabOtherNumMatrices( StringArray &asNames, string& strResult )
{
string strExecute;
StringArray asNamesTemp;
int nSize = asNames.GetSize();
asNamesTemp.SetSize( nSize );
for(int ii=0; ii<nSize; ii++)
{
asNamesTemp[ii] = STR_TEMP_ARRAY_NAME + ii;
strExecute.Format("%s=%s;"
"%s=double(%s);",
asNamesTemp[ii], asNames[ii], asNames[ii], asNamesTemp[ii]);
m_mlObj.Execute(strExecute);
}
BOOL bRet = GetMatlabRealMatrices( asNames, strResult );
for( ii=0; ii<nSize; ii++)
{
strExecute.Format("%s=%s;"
"clear %s",asNames[ii], asNamesTemp[ii],asNamesTemp[ii]);
m_mlObj.Execute(strExecute);
}
return bRet;
}
int AddMatlabNumArray(Worksheet& wks, string strMatLabVarName, bool bReplaceCurrent)
{
string strExecute;
strExecute.Format("%s=%s;"
"%s=double(%s);",
STR_TEMP_ARRAY_NAME,strMatLabVarName,strMatLabVarName,STR_TEMP_ARRAY_NAME );
string strRet = m_mlObj.Execute(strExecute);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -