How can I export variable to a text file in mex file ?
Mostrar comentarios más antiguos
Dear Friends,
I am working MATLAB Version 7.6.0.324 (R2008a) with Microsoft Visual C++ 2010 compiler.
I want to export my variable to a text file, here some lines of my c file:
/* amsubread6.c - MATLAB mex file for reading AMSU-B data
/* Usage: amsub = amsubread6(amsubfile)
#include "mex.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define NUM_CHANNELS 5
.
.
.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *tb_mat, *lat_mat, *lon_mat;
double *tb[NUM_CHANNELS], *lat, *lon;
int dims[2] = {NUM_CHANNELS, 1};
.
.
.
tb_mat = mxCreateCellArray(2, dims);
mxSetCell(tb_mat, channel, mxCreateDoubleMatrix(num_spots,num_scans, mxREAL));
tb[channel] = mxGetPr(mxGetCell(tb_mat, channel));
mxSetFieldByNumber(plhs[0], 0, 0, tb_mat);
lat_mat = mxCreateDoubleMatrix(num_spots,num_scans, mxREAL);
lat = mxGetPr(lat_mat);
mxSetFieldByNumber(plhs[0], 0, 1, lat_mat);
lon_mat = mxCreateDoubleMatrix(num_spots,num_scans, mxREAL);
lon = mxGetPr(lon_mat);
mxSetFieldByNumber(plhs[0], 0, 2, lon_mat);
.
.
.
How can I export tb[], lat, lon to a text file like this format :
lat lon tb[1] tb[2] tb[3] tb[4] tb[5]
Thanks for your help
2 comentarios
James Tursa
el 16 de Ag. de 2012
Do you mean lat[0] and lon[0]?
hadi
el 18 de Ag. de 2012
Respuestas (1)
José-Luis
el 16 de Ag. de 2012
Hello!
#include <iostream>
#include <fstream>
using namespace std;
ofstream myfile;
myfile.open ("my_file.txt");
//A loop will probably required here
myfile << "lat" << " " << "lon" << " " << val1 << " " << [...] << endl;
myfile.close();
Cheers!
5 comentarios
hadi
el 18 de Ag. de 2012
Editada: Walter Roberson
el 18 de Ag. de 2012
Walter Roberson
el 18 de Ag. de 2012
What file extension did you save the code as? <fstream> and other aspects of the code shown are for C++ and would not compile if saved to a .c file. Your original code is C code.
hadi
el 18 de Ag. de 2012
José-Luis
el 18 de Ag. de 2012
True, the my answer was C++. If you want plain C, then you can use fopen and fprintf.
hadi
el 27 de Ag. de 2012
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!