Skip to content

Commit

Permalink
Use real variable names in Matlab (.m) export
Browse files Browse the repository at this point in the history
  • Loading branch information
robbr48 committed Nov 28, 2023
1 parent 05cccf5 commit aaf1151
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
9 changes: 7 additions & 2 deletions HopsanGUI/LogVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion HopsanGUI/LogVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 33 additions & 28 deletions HopsanGUI/PlotTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,49 +536,54 @@ void PlotTab::exportToMatlab()

// Cycle plot areas
int nTotCurves=0;
for (int a=0; a<mPlotAreas.size(); ++a)
{
PlotArea *pArea = mPlotAreas[a];
QList<PlotCurve*> curves = pArea->getCurves();

QStringList addedVariablesX, addedVariablesY;
for(const auto &pArea : qAsConst(mPlotAreas)) {
// Cycle plot curves
for(int c=0; c<curves.size(); ++c)
{
fileStream << "x" << c+nTotCurves << "=["; //Write time/X vector
for(const auto &pCurve : qAsConst(pArea->getCurves())) {
if(pArea->hasCustomXData())
{
//! @todo need smart function to auto select copy or direct access depending on cached or not (also in other places)
QVector<double> xvec = pArea->getCustomXData()->getDataVectorCopy();
for(int j=0; j<xvec.size(); ++j)
{
if(j>0) fileStream << ",";
fileStream << xvec[j];
QString varName = pArea->getCustomXData()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration());
if(!addedVariablesX.contains(varName)) {
fileStream << varName << "=[";
QVector<double> xvec = pArea->getCustomXData()->getDataVectorCopy();
for(int i=0; i<xvec.size(); ++i) {
if(i>0) fileStream << ",";
fileStream << xvec[i];
}
fileStream << "];\n";
}
addedVariablesX.append(varName);
}
else
{
//! @todo what if not timevector then this will crash
QVector<double> time = curves[c]->getSharedTimeOrFrequencyVariable()->getDataVectorCopy();
for(int j=0; j<time.size(); ++j)
{
if(j>0) fileStream << ",";
fileStream << time[j];
QString varName = pCurve->getSharedTimeOrFrequencyVariable()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration());
if(!addedVariablesX.contains(varName)) {
fileStream << varName << "=[";
QVector<double> time = pCurve->getSharedTimeOrFrequencyVariable()->getDataVectorCopy();
for(int i=0; i<time.size(); ++i) {
if(i>0) fileStream << ",";
fileStream << time[i];
}
fileStream << "];\n";
}
addedVariablesX.append(varName);
}
fileStream << "];\n";

fileStream << "y" << c+nTotCurves << "=["; //Write data vector
QVector<double> data=curves[c]->getVariableDataCopy();
for(int k=0; k<data.size(); ++k)
{
if(k>0) fileStream << ",";
fileStream << data[k];

QString varName = pCurve->getSharedVectorVariable()->getSmartName("_")+"_"+QString::number(pCurve->getCurveGeneration());
addedVariablesY.append(varName);
fileStream << varName << "=["; //Write data vector
QVector<double> data=pCurve->getVariableDataCopy();
for(int i=0; i<data.size(); ++i) {
if(i>0) fileStream << ",";
fileStream << data[i];
}
fileStream << "];\n";
nTotCurves++;
}
// Increment number for next plot area
nTotCurves += curves.size();

}

// Write plot functions
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aaf1151

Please sign in to comment.