Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:33:38+00:00 2026-06-08T21:33:38+00:00

I have a .mat file which could be easily read by matlab, but I

  • 0

I have a .mat file which could be easily read by matlab, but I need to convert it a C++ readable .model file. is there a way to do it (by hands, or maybe programmatically)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:33:40+00:00Added an answer on June 8, 2026 at 9:33 pm

    You could load the data matrix in MATLAB as any regular MAT-file:

    load data.mat
    

    then use the MEX function libsvmwrite which comes with the libsvm MATLAB interface, to write it to the so called “sparse” format:

    libsvmwrite('data.txt', label_vector, instance_matrix)
    

    If you are talking about trained models not data, a quick search revealed this page (I haven’t personally tested it).


    EDIT:

    Ok, it appears that the code I mentioned needs some tweaking. Below is my modified version. I tested it using the latest libSVM-3.12, with VS2010 as compiler:

    svm_savemodel.c

    #include "../svm.h"
    #include "mex.h"
    #include "svm_model_matlab.h"
    
    static void fake_answer(mxArray *plhs[])
    {
        plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    }
    
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
        struct svm_model *model;
        char *filename;
        const char *error_msg;
        int status;
    
        // check input
        if(nrhs != 2) {
            mexPrintf("Usage: svm_savemodel(model, 'filename');\n");
            fake_answer(plhs);
            return;
        }
        if(!mxIsStruct(prhs[0])) {
            mexPrintf("model file should be a struct array\n");
            fake_answer(plhs);
            return;
        }
        if(!mxIsChar(prhs[1]) || mxGetM(prhs[1])!=1) {
            mexPrintf("filename should be given as char(s)\n");
            fake_answer(plhs);
            return;
        }
    
        // convert MATLAB struct to C struct
        model = matlab_matrix_to_model(prhs[0], &error_msg);
        if(model == NULL) {
            mexPrintf("Error: can't read model: %s\n", error_msg);
            fake_answer(plhs);
            return;
        }
    
        // get filename
        filename = mxArrayToString(prhs[1]);
    
        // save model to file
        status = svm_save_model(filename,model);
        if (status != 0) {
            mexWarnMsgTxt("Error occured while writing to file.");
        }
    
        // destroy model
        svm_free_and_destroy_model(&model);
        mxFree(filename);
    
        // return status value (0: success, -1: failure)
        plhs[0] = mxCreateDoubleScalar(status);
    
        return;
    }
    

    Assuming you compiled the above MEX file, here is an example usage:

    [labels, data] = libsvmread('./heart_scale');
    model = svmtrain(labels, data, '-c 1 -g 0.07');
    svm_savemodel(model, 'mymodel.model');
    

    The text file created looks like:

    mymodel.model

    svm_type c_svc
    kernel_type rbf
    gamma 0.07
    nr_class 2
    total_sv 130
    rho 0.426412
    label 1 -1
    nr_sv 63 67
    SV
    1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1 
    0.6646947579781318 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5 
    .
    .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on research model which outputs results to matlab's .mat file format, and
in my application I have a Mat file which i'd like to show in
Hey there. Basically, I have a batch-file, that goes through all model and texture
I have a .mat file in which I put data previously processed. When I
I have a .mat file that I want to open and see its contents.
i have a file, filedata.mat, containing a 1x1 struct with sub-levels that contain data
I have a file containing the information 0001:Jack:Jeremy:6:38.0 0002:Mat:Steve:1:44.5 0003:Jessy:Rans:10:50.0 0004:Van Red:Jimmy:3:25.75 0005:Peter:John:8:42.25 0006:Mat:Jeff:3:62.0
I have the following code to convert the type of the matrix using cv::Mat.convertTo()
Say I have a .mat file with several instances of the same structure, each
I have a binary image in Matlab, and I need the binary array(0 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.