如何在Matlab中读取文件? [英] How to read file in matlab?

查看:137
本文介绍了如何在Matlab中读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个txt文件,文件内容是数字行, 每行中都有5个浮点数,每个数字之间用逗号分隔. 例如:

I have a txt file, and the content of the file is rows of numbers, each row have 5 float number in it, with comma seperate between each number. example:

1.1,12,1.42562,3.5,2.2

1.1 , 12 , 1.42562, 3.5 , 2.2

2.1,3.3,3,3.333,6.75

2.1 , 3.3 , 3 , 3.333, 6.75

如何在Matlab中将文件内容读入矩阵? 到目前为止,我有这个:

How can I read the file content into matrix in matlab? So far I have this:

fid = fopen('file.txt');
comma  = char(',');
A = fscanf(fid, ['%f', comma]);
fclose(fid);

问题在于,它只会给我第一行以及当我 尝试写A的内容我得到这个:1.0e + 004 *一些数字

The problem is that it's only give me the first line and when I try to write the content of A I get this: 1.0e+004 * some number

有人可以帮我吗? 我想对于文件,我需要循环读取它,但是我不知道如何.

Can someone help me please? I guess that for the file I need to read it in a loop but I don't know how.

另一个问题:当我输出到A时,我得到了:

One more question: When I do output to A I get this:

A =

1.0e+004 *
4.8631         0         0         0    0.0001
4.8638   -0.0000   -0.0000    0.0004    0.0114
4.8647   -0.0000   -0.0000    0.0008    0.0109

我希望将文件中的值与矩阵中的值相同,如何使数字成为常规浮点数并且不这样格式化?还是矩阵中的数字实际上是浮动的,但输出只是这样显示?

I want the same values that in the file to be in the matrix, how can I make the numbers to be regular float and not formatted like this? Or are the numbers in the matrix actually float, but the output is just displayed like this?

推荐答案

MATLAB的内置 dlmread 函数将是您要完成的工作的简便得多的解决方案.

MATLAB's built-in dlmread function would be a much easier solution for what you want to accomplish.

A = dlmread('filename.txt',',') % call dlmread and specify a comma as the delimiter

这篇关于如何在Matlab中读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆