MATLAB Answers

How to make a .csv file with data values in .txt

Latest activity Answered by Star Strider
on 24 Jun 2019 at 17:48
Hallo,
I have a data .txt file with values as below,
0.000000e+00 0.000000e+00
1.005346e-04 1.842308e-20
2.006482e-04 6.750698e-20
3.000943e-04 3.313659e-19
4.002079e-04 6.749189e-19
5.003215e-04 1.001656e-18
6.004351e-04 1.332393e-18
7.005486e-04 1.622378e-18
8.006612e-04 3.978657e+01
9.001020e-04 2.101415e+03
1.000211e-03 5.359804e+03
1.100320e-03 8.372647e+03
I need to split them with some delimiter and export it to a .csv file with two columns so that I can plot the graph with Column 1 as X and Column two as Y.
Can anyone help me how to do it ?
Matlab R2015b

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 24 Jun 2019 at 17:48
 Accepted Answer

If you want to read your file using readtable, you need to do some processing on it afterwards:
T = readtable('data.txt','ReadVariableNames',0);
T(1:3,:) = [];
T1 = varfun(@str2double,T);
T1.Properties.VariableNames = {'X','Y'};
figure
plot(T1.X, T1.Y) % Plot Table
grid
Note that ‘T’ imports the numerical data as strings, so you need to convert them to numerical variables to use them.
You can also use textscan to read it:
fidi = fopen('data.txt');
for k = 1:3
hdrs{k} = fgetl(fidi); % Read & Store Headers
end
C = textscan(fidi,'%f%f', 'CollectOutput',1); % Read & Store Numerical Data (Cell Array)
fclose(fidi);
D = cell2mat(C); % Store Numerical Data As Matrix (Optional)
figure
plot(C{:}(:,1), C{:}(:,2)) % Plot Cell Array
grid
figure
plot(D(:,1), D(:,2)) % Plot Matrix
grid
I would just leave your file as it is, however. If you want to store only the numerical values, just save (link) it as a .mat file.

  0 Comments

Sign in to comment.


Answer by madhan ravi
on 24 Jun 2019 at 16:18
Edited by madhan ravi
on 24 Jun 2019 at 16:26

T = readtable('data.txt');
T1 = str2double(table2cell(T));
csvwrite('Wanted.csv',T1(~all(isnan(T1),2),:))
% after this the csv file can be read and it can be plotted
W = csvread('Wanted.csv');
plot(W(:,1),W(:,2))

  3 Comments

Hallo Madhan,
I have run the script. I get a following error, Do you get this error ?
Error using dlmread (line 143)
Empty format string is not supported at the end of a file.
Error in csvread (line 47)
m=dlmread(filename, ',', r, c);
Error in test2 (line 9)
W = csvread(wanted);
Regards,
Jaffrey Hudson
data= 'xxxxxxxxxxx\data.txt';
wanted='xxxxxxxxxxx\wanted.csv';
T = readtable(data);
T1 = str2double(table2cell(T));
csvwrite(wanted,T1(~all(isnan(T1),2),:))
% after this the csv file can be read and it can be plotted
W = csvread(wanted);
plot(W(:,1),W(:,2))
This was my script and the error was
Warning: Variable names were modified to make them valid MATLAB identifiers.
Error using dlmread (line 143)
Empty format string is not supported at the end of a file.
Error in csvread (line 47)
m=dlmread(filename, ',', r, c);
Error in test2 (line 9)
W = csvread(wanted);
No I didn't have any error messages

Sign in to comment.