-1

テキスト ファイルからデータを読み込んで、Matlab で 3D プロットを実行しようとしています。現在、私が得ているのは空白のプロットだけなので、データが正しく保存されていないか、まったく保存されていないと思います。また、すべてのベクトルの最後に 1.000000 を付けたくないので、どうすればそれを無視できますか? ありがとう。

ファイルは次のとおりです。

引用符

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++\testFile.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
4

1 に答える 1

1

コードが現在実行されている方法では、temp変数は各反復で空白です。textscan 行を次のように置き換えます

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: が含まれているため、最初の行を個別に処理する必要があります。

また、s の前に temp が空でないことを確認するチェックを追加することをお勧めしvertcatます。

于 2012-05-24T18:19:05.110 に答える