Reputation: 311
I have trouble with writing this Matlab code to a csv or text file. How can this be done? I want the three lines values (x,y,z):
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)
to be written to a text or csv file.
Alternatively, to use csvwrite
command, I am uncertain how to convert these results to a X by 3 matrix, so they can be written to a csvfile by the csvwrite
command in Matlab.
Help would be much appreciated.
Matlab matrix: 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
Upvotes: 0
Views: 63
Reputation: 1556
You need to fopen
a txt file to write, for example 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)
In the output.txt file:
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
Upvotes: 3