简体   繁体   English

将c ++代码中的向量写入文件,然后在matlab中读取

[英]writing a vector from c++ code into a file and then reading it in matlab

I have a vector of data type double generated in c++ and I am trying to write it into a text file so that I can then load the text file into matlab in order to plot the data from the vector versus data that is already in my matlab workspace. 我有一个用c ++生成的数据类型为double的矢量,我试图将其写入文本文件,以便随后可以将该文本文件加载到matlab中,以便从矢量中绘制数据与matlab中已经存在的数据作图工作区。 I am able to create a file called real_data2.txt, and the program runs without an error, but then when I try to load the file in Matlab or open it in a text reader, the contents of the file is gibberish so-to-speak. 我能够创建一个名为real_data2.txt的文件,并且程序运行时没有错误,但是当我尝试在Matlab中加载该文件或在文本阅读器中打开该文件时,该文件的内容简直是乱码。说话。 The code that I am using to create the file is: 我用来创建文件的代码是:

ofstream out("real_data2.txt");
    if(!out){
        cout << "Cannot open file.\n";
        return;
    }
for(int i = 0; i<N; i++){
    out.write((char *) &out2[i][0], N);
}
out.close();

where N is the length of out2. 其中N是out2的长度。 This successfully generates the 'real_data2.txt' file. 这将成功生成“ real_data2.txt”文件。 I then put the generated file into the matlab path and use A = textread('real_data2.txt' ) in matlab to try to read the file. 然后,我将生成的文件放入matlab路径,并在matlab中使用A = textread('real_data2.txt' )尝试读取该文件。 The error message that I receive is: 我收到的错误消息是:

Error using data read Trouble reading number from file (row 1, field 1) ==> ?> Uíl¯9@9ÊÝÖ(@9Ò»=£P<@¨CÁ\\n 使用数据读取时出错从文件读取错误(行1,字段1)==>?>Uílé9@9ÊÝÖ(@9Ò»=£P <@¨CÁ\\ n

Error in textread (line 175) [varargout{1:nlhs}]=dataread('file',varargin{:}); textread中的错误(第175行)[varargout {1:nlhs}] = dataread('file',varargin {:}); %#ok %#好

and when I double click the txt file I get a file containing text like: 当我双击txt文件时,我得到一个包含如下文本的文件:

?>Uíl¯9@9ÊÝÖ(@9Ò»=£P<@¨CÁ ?>Uíl¯9@9ÊÝÖ(@ 90»=£P <@ CA

' C@Bw|?N¯8@]H~=@7@7ÂÙjó?õ?ñXéWSO@¹U¥A° õ¿»D¾;@@¡ԨÊëQÀw®nr!úU@ß?7XÀjw©}NÀJA(×FÀ|ûó>%÷GÀa¬W¯7@·âÖ3Ð ÀÉ¿ x¨G@Ï¿U°ÜÁ6@ò5^->@qzáme[ÀKÆ]T^'À¼7CJK@EhúôP:ÀF¼¸ý@íÀ÷¾§ÎC@2w©^=AÀÛ«½ñðúr@ >ò(÷@B˶xÀË6Y7¸@s÷ãCÀ<µ} lÀ<uÄòÚtÀêGÂ0KZÀ~iò*~@$áu@Àq >¢Ò E{@Õbp«^8DÀ4©@Ëçc@nê'È1ÀÑJ?5|@?$éEy ÀUßÎzí@,u°<ä@^Q4íbÀ4õÆÚ@6S8(*ÀD$°ä?u@kn|½Àz >ÓvÀ'F@ôuc@þÃjàÂ:À KµÕv@ý/ú?ÁL]Àv°¶ó+R@íæ¿¥òC@ðvwjc@WÔÙjÀ¡ @y¿ò5d®I@M¸ö/þät@?ZÝd>@Cåh0~ÀLÛoE@cܼ¥À§ÝY.^Àù}øDzÀß®H\\XÀ¦ähöT~@ÍÚ\\åþÀá@Êà¤Ï»TÀAýx@r$Ü@u@Bã¿ l@?ß xç@½ô¶ %ÖbÀ{̱Àò#@HëSuÀ[ÙÑóÏ)w@ zû%ÀGK«7qiÀŦÊoÀúxeIÀy8nÓV@ØÕ3~yÀ³ >ú¹@vc@ñáĶmÀ &Q¹ig@?klgmÀ`Ìh&ûu@J «?kGiÀÂ|7kê@?y- @ªôg@ïíEY@\\Ayi?S\\@eظhã@ðô6fzÀ ©ås¬·@ROÅÍjÀÓâÒ×SJ Àýßv¨b >À(Ê- ߸Àów Ás@û?öÅèÀj¦Ðî@© ÎÀ¾&ñ~.U@_ÿz<~@qådøIÛz@4 ' C@Bw|?N¯8@]H~=@7@7ÂÙjó?õ?ñXéWSO@¹U¥A° õ¿»D¾;@@¡ԨÊëQÀw®nr!úU@ß?7XÀjw©}NÀJA(×FÀ|ûó>%÷GÀa¬W¯7@·âÖ3Ð @Ï¿U°ÜÁÁ6@ò5^-> @qzáme[ÀKÆ] T ^'À¼7CJK@EhúôP:ÀF¼¸ý@@ÀÀ÷ ¾§ÎC@ 2w©^ =AÁÛ«½ñðúr@>ò(÷@BËxÀË6Y7¸@ s÷ãCÀ<µ} lÀ<uÄòÚtÀêGÂ0KZÀ~iò*~@$áu@Àq >¢Ò E{@Õbp«^8DÀ4©@Ëçc@nê'È1ÀÑJ?5|@?$éEy @,u°<ä@ ^Q4íbÀ4õÆÚ@ 6S8(*ÀD$°ä?u @ k @ |½Àz>ÓvÀ'F@ôuc@þÃjàÂ:ÀKµÕv @ ý/ú?ÁL]Àv°¶ó+ R @íæ¿¥òC@ðvwjc@WÔÙjÀ¡@y¿ò5d®I@M¸ö/þät@?ZÝd> @Cåh0〜ÀLÛoE@cܼ¥¥§§Y。^ \\¹øDzÀß®H\\ XÀ¦ähöT〜@ÍÚ\\åþÀá@Êà¤Ï»TÀAýx@ r $Ü@ u @Bã¿l@?ßxç@½ô¶%ÖbÀ{̱À## @HëSuÀ[ÙÑóÏ] w @zû%ÀGK«7qiÀÅÊoÀúxeIyy8nÓV@ØÕ3〜yÀ³>ú¹@ vc @ñáĶ¶mÀ&Q¹ig@?klgmÀ`Ìh&ûu@ J«? hã@ðô6fzÀ©ås¬·@ROÅÍjÀÓ××SJÀýßv¨b>À(Ê-߸ÀówÁs@û?öÅèÀjÐÐ@@©Á¾&ñ~.U@_ÿz<〜@qådøIÛz@ 4 )Áe@:´ N?{À=ào¸@ÆlëíVÒÀ©ÀÍûY7|XÀ¬' Çä1ÀØ@Á?Çj@®¨k8À¨ )Áe@:´N?{À=ào¸@ÆlëíVÒÀÀÍûY7|XÀ'Çä1ÀØ@Á?Çj@®k8À¨

Any ideas where I am going wrong? 有什么想法我要去哪里吗? I don't know much about this topic, but I am assuming that it has something to do with trying to cast the location of the data to a char pointer but I am not sure and wouldn't know how to get around that since I have tried altering that line unsuccessfully with things like out.write((char *) out2[i][0], N); 我对该主题了解不多,但是我认为它与尝试将数据的位置转换为char指针有关,但是我不确定,因为我不知道该如何解决。尝试使用out.write((char *) out2[i][0], N);类的方法成功更改该行out.write((char *) out2[i][0], N); and out.write((double *) &out2[i][0], N); out.write((double *) &out2[i][0], N); which I realize now might not make too much sense...Anyways, any help would be greatly appreciated! 我意识到现在可能没有太大意义...无论如何,我们将不胜感激任何帮助! Let me know if you know how to fix this problem! 如果您知道如何解决此问题,请告诉我!

out << out2[i][0];

is the correct way. 是正确的方法。

Your output is binary. 您的输出是二进制的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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