From f4013449b4fc00d3f0cdb1094e8606f182a29cde Mon Sep 17 00:00:00 2001 From: Robert Braun Date: Tue, 28 Nov 2023 10:59:40 +0100 Subject: [PATCH] Use real variable names in Matlab (.m) export --- HopsanGUI/LogVariable.cpp | 9 ++++-- HopsanGUI/LogVariable.h | 2 +- HopsanGUI/PlotTab.cpp | 61 +++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/HopsanGUI/LogVariable.cpp b/HopsanGUI/LogVariable.cpp index aa56f37f5..051294c8d 100644 --- a/HopsanGUI/LogVariable.cpp +++ b/HopsanGUI/LogVariable.cpp @@ -323,11 +323,16 @@ QString VectorVariable::getFullVariableNameWithSeparator(const QString sep) cons return mpVariableDescription->getFullNameWithSeparator(sep); } -QString VectorVariable::getSmartName() const +QString VectorVariable::getSmartName(const QString sep) const { if (mpVariableDescription->mAliasName.isEmpty()) { - return mpVariableDescription->getFullName(); + if(sep.isEmpty()) { + return mpVariableDescription->getFullName(); + } + else { + return mpVariableDescription->getFullNameWithSeparator(sep); + } } else { diff --git a/HopsanGUI/LogVariable.h b/HopsanGUI/LogVariable.h index cdedbfc5d..f7313b143 100644 --- a/HopsanGUI/LogVariable.h +++ b/HopsanGUI/LogVariable.h @@ -137,7 +137,7 @@ class VectorVariable : public QObject const QString &getAliasName() const; QString getFullVariableName() const; QString getFullVariableNameWithSeparator(const QString sep) const; - QString getSmartName() const; + QString getSmartName(const QString sep="") const; const SharedSystemHierarchyT getSystemHierarchy() const; const QString &getModelPath() const; const QString &getComponentName() const; diff --git a/HopsanGUI/PlotTab.cpp b/HopsanGUI/PlotTab.cpp index 3bf31c14e..97a6b8f6a 100644 --- a/HopsanGUI/PlotTab.cpp +++ b/HopsanGUI/PlotTab.cpp @@ -536,49 +536,54 @@ void PlotTab::exportToMatlab() // Cycle plot areas int nTotCurves=0; - for (int a=0; a curves = pArea->getCurves(); - + QStringList addedVariablesX, addedVariablesY; + for(const auto &pArea : qAsConst(mPlotAreas)) { // Cycle plot curves - for(int c=0; cgetCurves())) { if(pArea->hasCustomXData()) { //! @todo need smart function to auto select copy or direct access depending on cached or not (also in other places) - QVector xvec = pArea->getCustomXData()->getDataVectorCopy(); - for(int j=0; j0) fileStream << ","; - fileStream << xvec[j]; + QString varName = pArea->getCustomXData()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration()); + if(!addedVariablesX.contains(varName)) { + fileStream << varName << "=["; + QVector xvec = pArea->getCustomXData()->getDataVectorCopy(); + for(int i=0; i0) fileStream << ","; + fileStream << xvec[i]; + } + fileStream << "];\n"; } + addedVariablesX.append(varName); } else { //! @todo what if not timevector then this will crash - QVector time = curves[c]->getSharedTimeOrFrequencyVariable()->getDataVectorCopy(); - for(int j=0; j0) fileStream << ","; - fileStream << time[j]; + QString varName = pCurve->getSharedTimeOrFrequencyVariable()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration()); + if(!addedVariablesX.contains(varName)) { + fileStream << varName << "=["; + QVector time = pCurve->getSharedTimeOrFrequencyVariable()->getDataVectorCopy(); + for(int i=0; i0) fileStream << ","; + fileStream << time[i]; + } + fileStream << "];\n"; } + addedVariablesX.append(varName); } - fileStream << "];\n"; - fileStream << "y" << c+nTotCurves << "=["; //Write data vector - QVector data=curves[c]->getVariableDataCopy(); - for(int k=0; k0) fileStream << ","; - fileStream << data[k]; + + QString varName = pCurve->getSharedVectorVariable()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration()); + addedVariablesY.append(varName); + fileStream << varName << "=["; //Write data vector + QVector data=pCurve->getVariableDataCopy(); + for(int i=0; i0) fileStream << ","; + fileStream << data[i]; } fileStream << "];\n"; + nTotCurves++; } // Increment number for next plot area - nTotCurves += curves.size(); - } // Write plot functions @@ -610,7 +615,7 @@ void PlotTab::exportToMatlab() else fileStream << "plot"; } - fileStream << "(x" << c+nTotCurves << ",y" << c+nTotCurves << ",'-" << matlabColors[c%6] << "','linewidth'," << curves[c]->pen().width() << ")\n"; + fileStream << "(" << addedVariablesX[c] << "," << addedVariablesY[c] << ",'-" << matlabColors[c%6] << "','linewidth'," << curves[c]->pen().width() << ")\n"; } fileStream << "hold off\n"; // Increment number for next plot area