简体   繁体   English

在Matlab中读取值文件

[英]read value file in Matlab

Currently I am using read comma-separated value (CSV) file which is csvread in my code. 目前,我正在使用读取的逗号分隔值(CSV)文件,该文件在我的代码中为csvread what if I want to read value row by row, which my value is not seperated by comma? 如果我想逐行读取值,而不用逗号分隔值,该怎么办?

This is my code for save data: 这是我保存数据的代码:

%% Save data to .txt
fid=fopen('MyFile1.txt','w');
fprintf(fid, '%f \n', AngleValue');
fclose(fid);

This is the save values 这是保存值

在此处输入图片说明

Then I run a code using csvread 然后我使用csvread运行代码

I = csvread('MyFile1.txt');

but I got an error. 但我有一个错误。 can anyone give an idea, probably I should change how I save my data or how can I read the data row by row. 任何人都可以提出一个想法,也许我应该更改保存数据的方式或如何逐行读取数据。 Thanks 谢谢

There are other alternatives to csvread . 还有其他替代csvread Try dlmread instead 改用dlmread

I = dlmread('MyFile.txt');

A more general approach, 更一般的方法
Why are you writing AngleValue as text files? 为什么将AngleValue作为文本文件编写? Why not use save and load in a more Matlab-ish fashion? 为什么不以Matlab式的方式使用saveload read and write in binary format, tailored for Matlab use? 以二进制格式读取和写入,是否适合Matlab使用?

 save('MyFile.mat','AngleValue'); %// save to binary Mat file

Once you saved the variable, you can read it: 保存变量后,即可读取它:

 load('MyFile.mat'); %// that's it!

use 采用

A = fscanf(fileID,formatSpec)

more info 更多信息

Problem solved. 问题解决了。 I found out that i just have to put coma (,) when i fprintf my saved data. 我发现在我保存的数据fprintf时只需要输入逗号(,)。

My previous code: 我以前的代码:

fprintf(fid, '%f \n', AngleValue');

My corrected code: 我更正的代码:

fprintf(fid, '%f,', AngleValue');

So now i can use csvread in my program. 所以现在我可以在程序中使用csvread了。

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

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