简体   繁体   中英

How to export data from C++ to MatLab

I have written a c++ program which outputs a list of random numbers. I was asked to export these numbers to MatLab in order to produce a histogram and other graphics. How can I do this? (I'm a beginner; please specify the extra files and steps I need to add). Thank you.

Here is what I did from the matlab console ( input.txt contains 3 integers values):

>> f=fopen('input.txt','rt')

f =

     3

>> fscanf(f,'%d')

ans =

        1234
       23435
         888

>> fclose(f)

ans =

     0

>> 

To sum it up:

f=fopen('input.txt','rt');
integerList = fscanf(f,'%d');
fclose(f);

For more details about the functions you can use doc or help in the Matlab console:

doc fscanf
help fscanf

You can just print out the numbers into a text file. If you print them in one line, separated by spaces or commas, you can read them into a 1-D Matlab array using the load command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM