Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use real variable names in Matlab (.m) export #2190

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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