c语言编程无法生成dat文件格式,c++ - 无法从.dat文件读取数据(从Simulink创建的VS2012 C ++项目) - 堆栈内存溢出...

编辑:谢谢,对那些感兴趣的人的固定代码:ert_main.cpp:

#include /* This ert_main.c example uses printf/fflush */

#include "Rx.h" /* Model's header file */

#include "rtwtypes.h" /* MathWorks types */

#include

#include

#include

#include

#include

#include

//#include //for writing results to file

//#include //doesn't work

#include

#define lengthOFSignal 5000

在主要功能上:

using namespace std;

std::vector<:string> words;

std::string word;

ifstream iFile;

string path = __FILE__; //gets source code path, include file name

path = path.substr(0,1+path.find_last_of('\\')); //removes file name

string testFileName = "LEGACY_R12_800BITS_40MHz.dat";

path+= testFileName; //adds input file to path

int signal_length=0;

std::vector real_part_VEC, imag_part_VEC;

std::istringstream ss;

real_T real_temp, imag_temp;

iFile.open(path,ios::binary );

//iFile.open(path.c_str(), ios::binary);

if (iFile.is_open()) {

while (std::getline(iFile, word)) {

words.push_back(word);

}

iFile.close();

}

signal_length=words.size();

for(int i=0; i< signal_length;i++)

{

ss.str(words[i]);

ss >> real_temp >> imag_temp;

real_part_VEC.push_back(real_temp);

imag_part_VEC.push_back(imag_temp);

}

real_T real_part[lengthOFSignal];

real_T imag_part[lengthOFSignal];

for (int i=0; i < signal_length; i++) {

real_part[i]=real_part_VEC[i];

imag_part[i]=imag_part_VEC[i];

}

/* Initialize model */

Rx_initialize(real_part,imag_part,signal_length);

旧代码和问题:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS91ckNIdy5qcGc= .dat文件看起来像是带有数字(以空格隔开)的两个直列

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9rckJNUS5qcGc=

我收到有关循环读取数据的错误(strtok-> atof(空指针))

编辑了ert_main.cpp主要功能:

#include /* This ert_main.c example uses printf/fflush */

#include "Rx.h" /* Model's header file */

#include "rtwtypes.h" /* MathWorks types */

#include

#include "mat.h"

#define lengthOFSignal 5000

#define SizeOfLine 35

int_T main(int_T argc, const char *argv[])

{

/* Unused arguments */

(void)(argc);

(void)(argv);

int i=0;

char bFileName[] = "QPSK_SISO_802_11_packet_awgn_fr_shft.dat";

char chr[SizeOfLine*lengthOFSignal];

char *token;

real_T real_part[lengthOFSignal];

real_T image_part[lengthOFSignal];

int signal_length=0;

std::ifstream iFile(bFileName, std::ios::binary);

iFile.getline(chr,100);

for(int i=0; i

if (chr== NULL) break;

token= strtok(chr," ");

real_part[i]=atof(token); // real part.---problem occurs here!!!!

token= strtok(NULL," ");

image_part[i]=atof(token);// imaginary part.

iFile.getline(chr,100);

signal_length++;

}

iFile.close();

/* Initialize model */

Rx_initialize(real_part,image_part,signal_length);

/* Attach rt_OneStep to a timer or interrupt service routine with

* period 40.0 seconds (the model's base sample time) here. The

* call syntax for rt_OneStep is

*

* rt_OneStep();

*/

printf("Warning: The simulation will run forever. "

"Generated ERT main won't simulate model step behavior. "

"To change this behavior select the 'MAT-file logging' option.\n");

fflush((NULL));

while (rtmGetErrorStatus(Rx_M) == (NULL)) {

/* Perform other application tasks here */

rt_OneStep();

if(Rx_Y.EVM!=0) break;

}

/* Disable rt_OneStep() here */

/* Terminate model */

Rx_terminate();

return 0;

}

链接到解决方案(VS 2012) 链接到项目/解决方案

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例MATLAB脚本,可以通过枚举从Excel生成.m文件并将其用作Simulink数据字典: ```matlab % 读取Excel文件 [num,txt,raw] = xlsread('example.xlsx'); % 获取枚举类型名称和值 enum_name = txt{1,1}; enum_values = txt(2:end,1); % 创建.m文件 fid = fopen([enum_name '.m'],'w'); fprintf(fid,'classdef %s\n',enum_name); fprintf(fid,' enumeration\n'); % 写入每个枚举值 for i = 1:length(enum_values) fprintf(fid,' %s,\n',enum_values{i}); end % 关闭.m文件 fprintf(fid,' end\n'); fprintf(fid,'end\n'); fclose(fid); % 生成Simulink数据字典 matlab_imported = Simulink.importExternalCTypes('example.h'); matlab_struct = struct('EnumType',enum_name,'HeaderFile','example.h'); Simulink.data.dictionary.create('example.sldd'); Simulink.data.dictionary.load('example.sldd'); Simulink.data.dictionary.addSection(enum_name); Simulink.data.dictionary.setValue([enum_name '.' enum_values{1}],matlab_imported.(enum_values{1}),matlab_struct); for i = 2:length(enum_values) Simulink.data.dictionary.setValue([enum_name '.' enum_values{i}],matlab_imported.(enum_values{i}),matlab_struct); end Simulink.data.dictionary.save; ``` 上述脚本假设已经有一个名为“example.xlsx”的Excel文件,其中第一列包含枚举类型名称和值。脚本将创建一个名为“枚举类型名称.m”的MATLAB类定义,并使用Simulink.importExternalCTypes函数从C头文件中导入枚举类型。然后,脚本将创建一个Simulink数据字典,并将每个枚举值添加到该数据字典中。最后,数据字典将保存到名为“example.sldd”的文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值