简体   繁体   English

MATLAB编码器支持的csvread函数

[英]csvread function supported by MATLAB coder

I have a MATLAB script that contains a csvread call to read in data from a csv file. 我有一个包含csvread调用的MATLAB脚本,该调用从csv文件中读取数据。 Now I'm trying to compile my MATLAB script into C using MATLAB Coder. 现在,我正在尝试使用MATLAB Coder将我的MATLAB脚本编译为C。 However, csvread is not a member of the supported functions for C code generation. 但是, csvread 不是 C代码生成受支持函数的成员

The only read from file function I found is fread which only reads in binary file. 我发现唯一从文件读取功能是fread ,它仅读取二进制文件。 Is there a way to use fread to read a csv file or is there any way around this? 有没有一种方法可以使用fread读取csv文件,或者有什么解决办法?

Sample MATLAB script: 示例MATLAB脚本:

data = csvread('data.csv');

Sample csv file: 样本csv文件:

1
2
3
4

Sure, you can read in a .csv file with fread , you'll just need to process it yourself. 当然,您可以使用fread读取.csv文件,只需要自己处理即可。 Let's say you have myfile.csv , containing the text 1, 2, 3, 4 . 假设您有myfile.csv ,其中包含文本1, 2, 3, 4

>> fid = fopen('myfile.csv','r');
>> a = fread(fid, 'char')'
a =
    49    44    32    50    44    32    51    44    32    52    13    10
>> b = char(a)
b =
1, 2, 3, 4
>> fclose(fid);

All those commands are supported by MATLAB Coder. 所有这些命令均受MATLAB Coder支持。 b is now a string containing the text 1, 2, 3, 4 . b现在是一个包含文本1, 2, 3, 4的字符串。

You'll now need to process that string to extract the numbers from between the commas. 现在,您需要处理该字符串以从逗号之间提取数字。 That's the portion of csvread that isn't supported by MATLAB Coder, as within csvread it uses regular expressions, which aren't supported by MATLAB Coder. 这是MATLAB Coder不支持的csvread的一部分,因为在csvread它使用的是正则表达式,而MATLAB Coder不支持。

However, you'll probably find it easier than you might think, as the regular expressions within csvread have to cover the general case for any .csv file (which might include text fields as well as numbers, and fields that are quoted and can have commas within the quotes, and maybe even Unicode text as well). 但是,您可能会发现它比您想象的要容易,因为csvread的正则表达式必须涵盖任何.csv文件的一般情况(该文件可能包括文本字段以及数字以及带引号且可以包含引号内的逗号,甚至还有Unicode文本)。 If you just have numbers, commas, and spaces, shouldn't be too hard - just iterate through the text character-by-character, stop each time you find a comma, and call str2double on the portion so far. 如果您只有数字,逗号和空格,则不要太难-逐个字母地遍历文本,每次找到逗号时都停止,并到目前为止str2double一部分调用str2double

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

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