该代码有多个需要解决的问题。这里有几件事要改进:
- Thefopen文件指出支撑材is aninteger file identifier .... The variable支撑材does notcontainthe file data, it is merely a参考到打开文件。它也是标量值。因此,您试图索引支撑材as if it were a data array doesn't make any sense.
- Over-complicated method of reading a text file: first textscan, then cellfun calling textscan again, all just to avoid some header lines? Instead you should readtextscan的文档,并使用HeaderLines选择,这样:
fid = fopen('filename.txt','r');
data = textscan(fid,'%F',“定界符”,'\t','HeaderLines',20);
FCLOSE(FID)
I also removed the
CollectOutput
option, as this is superfluous if there is only one format specifier. For the same reason the delimiter doesn't really make sense either. If you have multiple values per line, then you need to specify them in the
formatSpec
: there are plenty of examples in the documentation.
Starting in R2018b, you can use the “rmmissing” function to remove “NaN” values from an array. For example, consider the following:
A = [1,NaN,2];
B = rmmissing(A)
The result is the vector “B = [1 2]”.
在R2018A及以后,使用“ ISNAN”函数:
A = [1,NaN,2];
B = A(~isnan(A))