Matlab写入文件或转换为矩阵

时间:2018-11-02 22:36:09

标签: matlab printf

我在将此Matlab代码写入csv或文本文件时遇到麻烦。如何才能做到这一点?我想要三行值(x,y,z):

  1. fprintf('%。2f,%。2f,%。2f \ n',(i-2)* 5,A(j,1),0)

  2. fprintf('%。2f,%。2f,%。2f \ n',A(j,i)+(i-2)* 5,A(j,1),0)

  3. fprintf('%。2f,%。2f,%。2f \ n',A(j,i)+(i-2)* 5,A(j,1)-5,0)

要写入文本或csv文件。

或者,要使用csvwrite命令,我不确定如何将这些结果转换为X by 3矩阵,因此可以通过Matlab中的csvwrite命令将它们写入csvfile。

我们将不胜感激。

Matlab矩阵: https://drive.google.com/file/d/1P5h6lhs0O2fxFcxHabAO4hU84UqAZD1m/view?usp=sharing

clear all

%num = xlsread('Workbooklol.xlsx');

load('matlab_matrix.mat')

L=length(num);

num2=num(4:72,:);

L2=length(num2);
L3=length(num2(1,:));

%
for i=1:13;
    for j=1:3;
        if isnan(num2(i,j)) ~= 1;

            A(i,j)=num2(i,j);

        end     
    end    
end


for i=2:3;

    for j=1:13;

        if A(j,i) > 0


          fprintf('%.2f,%.2f,%.2f \n',(i-2)*5,A(j,1),0)
          fprintf('%.2f,%.2f,%.2f \n', A(j,i)+(i-2)*5,A(j,1),0)
          fprintf('%.2f,%.2f,%.2f \n', A(j,i)+(i-2)*5,A(j,1)-5,0)


        end




    end

    fprintf('(command) \n')
    fprintf('line \n')

end

1 个答案:

答案 0 :(得分:3)

您需要fopen编写一个txt文件,例如output.txt。

clear all

%num = xlsread('Workbooklol.xlsx');

load('matlab_matrix.mat')

L=length(num);

num2=num(4:72,:);

L2=length(num2);
L3=length(num2(1,:));

%
for i=1:13;
    for j=1:3;
        if isnan(num2(i,j)) ~= 1;

            A(i,j)=num2(i,j);

        end     
    end    
end

fileID = fopen('output.txt','w');

for i=2:3;

    for j=1:13;

        if A(j,i) > 0


          fprintf(fileID,'%.2f,%.2f,%.2f \n',(i-2)*5,A(j,1),0);
          fprintf(fileID,'%.2f,%.2f,%.2f \n', A(j,i)+(i-2)*5,A(j,1),0);
          fprintf(fileID,'%.2f,%.2f,%.2f \n', A(j,i)+(i-2)*5,A(j,1)-5,0);


        end




    end

    fprintf(fileID,'(command) \n');
    fprintf(fileID,'line \n');

end

fclose(fileID)

在output.txt文件中:

0.00,745.00,0.00 
4.80,745.00,0.00 
4.80,740.00,0.00 
0.00,740.00,0.00 
3.00,740.00,0.00 
3.00,735.00,0.00 
(command) 
line 
5.00,747.00,0.00 
16.00,747.00,0.00 
16.00,742.00,0.00 
5.00,742.00,0.00 
18.00,742.00,0.00 
18.00,737.00,0.00 
(command) 
line