import matrix from excel to matlab

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

// Function to import matrix from Excel to Matlab
void importMatrixFromExcelToMatlab(const std::string& fileName, std::vector<std::vector<double>>& matrix);

int main() {
    // Specify the Excel file name with the matrix data
    std::string fileName = "your_excel_file.xlsx";

    // Declare a 2D vector to store the matrix
    std::vector<std::vector<double>> matrix;

    // Call the function to import the matrix from Excel to Matlab
    importMatrixFromExcelToMatlab(fileName, matrix);

    // Display the imported matrix
    for (const auto& row : matrix) {
        for (const auto& element : row) {
            std::cout << element << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

// Function to import matrix from Excel to Matlab
void importMatrixFromExcelToMatlab(const std::string& fileName, std::vector<std::vector<double>>& matrix) {
    // Open the Excel file for reading
    std::ifstream inputFile(fileName);

    if (!inputFile.is_open()) {
        std::cerr << "Error opening file: " << fileName << std::endl;
        return;
    }

    // Read each line from the file
    std::string line;
    while (std::getline(inputFile, line)) {
        // Split the line into tokens based on delimiter (e.g., comma)
        size_t pos = 0;
        std::string token;
        std::vector<double> row;

        while ((pos = line.find(',')) != std::string::npos) {
            token = line.substr(0, pos);
            // Convert the token to a double and add it to the row
            row.push_back(std::stod(token));
            line.erase(0, pos + 1);
        }

        // Convert the remaining part of the line to a double and add it to the row
        row.push_back(std::stod(line));

        // Add the row to the matrix
        matrix.push_back(row);
    }

    // Close the file
    inputFile.close();
}