How can I export variable to a text file in mex file ?

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 Comments

Do you mean lat[0] and lon[0]?
Hi James,
My C files has several outputs : ans =
tb: {5x1 cell}
lat: [90x2416 double]
lon: [90x2416 double]
solz: [90x2416 double]
satz: [90x2416 double]
az: [90x2416 double]
ssa: [90x2416 double]
roll: [1x2416 double]
pitch: [1x2416 double]
yaw: [1x2416 double]
time: [1x2416 double]
sdate: [1x2416 double]
dir: [1x2416 double]
I want to export these outputs to a text file.

Sign in to comment.

Answers (1)

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 Comments

Hi Jose-Luis,
when I used
#include <fstream>
some errors found like these lines:
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\cmath(19) : error C2061: syntax error : identifier 'acosf'
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\cmath(19) : error C2059: syntax error : ';'
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.
yes, you right Walter, but I tested for Jose-Luis to show him the errors !
True, the my answer was C++. If you want plain C, then you can use fopen and fprintf.
I used these codes :
#include <stdio.h>
char Tfilename;
int Tfile;
Tfilename = "log.txt";
Tfile = open(Tfilename, O_WRONLY);
if i use
mexPrintf(Tfile,"%f ", ...
Matlab will be crashed
for printf, fprintf or sprintf
error is :
amsubread6.obj : error LNK2019: unresolved external symbol _fPrintf referenced in function _mexFunction
amsubread6.mexw32 : fatal error LNK1120: 1 unresolved externals
Can you tell me, what is wrong ?

Sign in to comment.

Asked:

on 16 Aug 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!