1

What is a simple translation in code for 'textscan' from matlab into C or C++? I am using Ubuntu and I am trying to translate a Matlab code into C++. Thank you very much.

Amro
  • 123,847
  • 25
  • 243
  • 454
js0823
  • 1,843
  • 8
  • 24
  • 36
  • no I think fscanf is similar to fscanf in matlab. Textscan in matlab searches for a format of code. it has some higher capabilities. – SAHM Jul 30 '14 at 19:25

2 Answers2

2

First, the answer is not the same if you're using C or if you're using C++. These are different programming languages.

Matlab is a much higher-level language than C and C++. In Matlab textscan reads from files or strings. C and C++ have different mechanisms for that.

To read from a file :

In C, you should use the FILE object and its associated functions (fopen, fgets ...) from the header file : stdio.h.

In C++, you should use std::ifstream from the <fstream> header file. For formatted input use the >> operator.

To read from a string :

In C, you might want to look at the functions in the string.h header.

In C++, the better way is to use the std::istringstream class from the sstream header file.

fouronnes
  • 3,838
  • 23
  • 41
  • Can you elaborate on reading file using ifstream and >>? How does ">>" work like matlab's textscan? – js0823 Mar 10 '11 at 17:07
1

It is fscanf. You will need to #include <stdio.h> and open FILE objects with fopen to use it.

Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59