English 中文(简体)
Matlab: 从文件存储数据
原标题:Matlab: storing data from a file

我试图从文本文件中读取数据, 并在 Matlab 中做一个 3D 图。 目前, 我得到的只是一个空白图, 所以我猜测数据没有被正确或完全存储。 另外, 我不想在每一个矢量的结尾处看到 1000 000, 这样我怎么能忽略它呢? 谢谢 。

以下是文件 :

块角

TechEdge4:<152.266724,173.189377,27.995975>  1.000000
<117.880638,156.116531,27.999983>  1.000000
<129.849899,59.195660,27.999983>  1.000000
<249.321121,60.605404,27.999983>  1.000000
<224.120361,139.072739,28.000668>  1.000000
<171.188950,143.490921,56.933430>  1.000000
<171.188950,143.490921,83.548088>  1.000000
<171.188950,143.490921,27.999985>  1.000000

这是代码:

file = fopen( C:Program Files (x86)Notepad++	estFile.txt ); % open text file

tline = fgetl(file); % read line by line and remove new line characters

% declare empty arrays
CX = [];
CY = [];
CZ = [];

while ischar(tline) % true if tline is a character array

    temp = textscan(tline, %n%n%n ,  delimiter , , );

    % convert all the cell fields to a matrix
    CX = vertcat(CX, cell2mat(temp));
    CY = vertcat(CY, cell2mat(temp));
    CZ = vertcat(CZ, cell2mat(temp));

    tline = fgetl(file);
end

fclose(file); % close the file

plot3(CX, CY, CZ) % plot the data and label the axises
xlabel( x )
ylabel( y )
zlabel( z ) 
grid on
axis square
最佳回答

您的代码运行方式, 您的 temp 变量在每迭代中为空白。 将文本扫描行替换为

temp = cell2mat(textscan(tline,  <%n,%n,%n> ));

然后是CX,CY,CZ线,

CX = vertcat(CX, temp(1));
CY = vertcat(CY, temp(2));
CZ = vertcat(CZ, temp(3));

当然,你需要单独处理第一行,因为它有TechEdge4:其中的东西。

我还建议增加一张支票,以确保在 vercat 之前临时性不是空的。

问题回答

暂无回答




相关问题
Is There Any Way Without File.ReadAllText?

As I love to develop file I/O applications and my new development platform is Windows Mobile. Is there any way to read all the file, without using File.ReadAllText? Because Windows Mobile doesn t ...

File paths in Java (Linux)

I have created a Java application that loads some configurations from a file conf.properties which is placed in src/ folder. When I run this application on Windows, it works perfectly. However when I ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签