如果从EXCEL复制数据,strtok不起作用

我使用C来在Matlab中调用一个DLL。 我的代码读取一个文件,然后准备input,以调用DLL。 这是代码。 当程序读取我写的.txt文件时,它正常工作。 但是,如果我从Excel粘贴数据不起作用。 例如,如果我将这些数据从EXCEL粘贴到test.txt文件中:“10.2 20 3 4 10.4 30 5 6”,我使用debugging器,我看到第一次迭代是正确的,因为inputs_mdl取值为10.2,20,3和4.然而在第二个它需要“10.4 \ t30 \ t \ 5 \ t6 \ t”。 结果,第二个代码之后的代码从未被执行。 EXCEL如何保持strtok正确地检测分离器? 谢谢!

if ((mdl_initialize && mdl_step && mdl_terminate && mdl_Uptr && mdl_Yptr)) { /* === user application initialization function === */ mdl_initialize(1); /* insert other user defined application initialization code here */ /* === user application step function === */ file_acquis=fopen("test.txt","r+"); file_outputs=fopen("outputs.txt","r+"); if ((file_acquis==NULL) || (file_outputs==NULL)) { printf ("Check files path"); return(-1); } else { //Checking the file until EOF while(fgets(input, 250, file_acquis)) { //Getting token from each line read inputs_mdl=strtok(input,separator); inputs_model[0]=atof(inputs_mdl); //Preparing inputs while ((inputs_mdl=strtok(NULL,separator)) !=NULL) { inputs_model[k]=atof(inputs_mdl); bucle=1; k++; } k=1; //Init struct if (bucle==1) { //Init structure time_elapsed= inputs_model[0];// mdl_Uptr->var1 =inputs_model[1];// mdl_Uptr->var2 =inputs_model[2];// //Call dll mdl_step(); printf ("retorno matlab"); printf ("%f\n",mdl_Yptr->output1); printf ("%f\n",mdl_Yptr->output2); //Getting outputs sprintf(string_file_output[0],"%f", mdl_Yptr->output1); sprintf(string_file_output[1],"%f", mdl_Yptr->output2); //Convert float to string. INPUTS for (z=0;z<INPUTS;z++) sprintf(string_file[0],"%f", inputs_model[z]); //Concanate for (z=1;z<INPUTS;z++) { strcat(string_file[0], " "); strcat(string_file[0], string_file[z]); } for (z=1;z<OUTPUTS;z++) { strcat(string_file[0], " "); strcat(string_file[0], string_file_output[z]); } //Writting end of line strcat(string_file[0], "\n"); printf ("%s\n", string_file[0]); //Write the file fputs(string_file[0],file_outputs); bucle=0; } } //Closing the file fclose(file_acquis); fclose(file_outputs); } mdl_terminate(); /* insert other user defined application termination code here */ } else { printf("Cannot locate the specified reference(s) in the shared library.\n"); return(-1); } return(CLOSELIB(handleLib)); 

}
}

Excel将电子表格数据保存为文本时,默认情况下将其保存为制表符分隔的格式,但听起来像Matlab将其保存为空格分隔的格式。 您可以将Excel电子表格保存为CSV文件,但我不确定空格分隔(尽pipe编写VBAmacros很容易)。 一种修复方法是将数据复制到记事本中, 将空格replace为制表符 ,然后将结果传输到正在尝试读取的文件中。 另一种方法是编写稍微复杂一点的C代码,可以在空格或制表符上分割(注意,你可以通过strtok多于一个分隔符)。