convert csv file and write to another .m file

17 vues (au cours des 30 derniers jours)
Arif Ahmed
Arif Ahmed le 5 Juil 2020
Commenté : Walter Roberson le 7 Juil 2020
Hello,
I am having problem reading in this file to matlab.
Data = readtable('100Cr6.csv','NumHeaderlines',0,'DecimalSeparator',',');
FID = fopen('kf100CR6.m', 'w');
fwrite(FID, Data, 'char');
fclose(FID);
i use the following code but it does not run successfully?
every time showing an error message:
"Error using fwrite
Cannot write value: unsupported class table
Error in convertCSV (line 15)
fwrite(FID, Data, 'char');"
i want every data in double notation (like 1231.2354) format in a .m file. so that i can use these as columns or rows of matrix.
Does anyone have a solution to this?
Many thanks.
  2 commentaires
Walter Roberson
Walter Roberson le 6 Juil 2020
you have columns that have three parts separated by semi-colon. If those are acting as decimal points then you have too many of them. If they are acting as group separation then you appear to be missing a decimal separation.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Juil 2020
filename = '100Cr6.csv';
S = fileread(filename);
S = regexprep(S, {',', ';'}, {'.', ','});
mdata = cell2mat( textscan(S, '', 'HeaderLines', 1) );
  4 commentaires
Arif Ahmed
Arif Ahmed le 6 Juil 2020
output of 'mdata(:,1)' should be like the followings:
and these are original data for the first column in the 'kf100CR6.m' or '100Cr6.csv' file:
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
.
.
.
.......
Walter Roberson
Walter Roberson le 7 Juil 2020
No, it should not be. Your input data has 0,1 for the first column, but you take log() of that to create the new first column. The result is not going to be 0,1 .
Look at
exp(mdata(1:10,1))
after my suggested code, and you will see
0.1
0.1
0.1
0.1
0.1
0.1
0.1
0.1
0.1
0.1
If this is not acceptable, then you need to tell us how you want the input "0,1" to be interpreted. Is it a pair of numbers 0 and 1, and you want log() of the first column to be log of 0 giving you -infinity, and log of the second part (1) to give you 0 ? Is it to be interpreted as 0 times 1 plus 1 times 1/10 with the comma acting as the decimal separator ? Are we intended to somehow understand the input "0,1" as indicating a number such that the result of the log() you do gives 0.1 -- that is, is the comma in "0,1" intended to indicate that the input number is to be intepreted as exp(0.1) ?
Or is the issue that output lines such as mdata(1,:)
-2.30258509299405 -3.2188758248682 0.04 20 6.12844000213506
use decimal points, and that you need the output to instead be
-2,30258509299405 -3,2188758248682 0,04 20 6,12844000213506
?

Connectez-vous pour commenter.

Plus de réponses (2)

jonas
jonas le 6 Juil 2020
Error message says that table class is not supported. You can pass the content of the table or just read the data as a matrix directly. I would try
Data = readmatrix('100Cr6.csv','NumHeaderlines',1,'DecimalSeparator',',')
dlmwrite('filename.m', Data)
  5 commentaires
Arif Ahmed
Arif Ahmed le 6 Juil 2020
still getting same problem as mentioned above!
jonas
jonas le 6 Juil 2020
I am getting the expected output.
Data = readmatrix('100Cr6.csv','NumHeaderlines',1,'DecimalSeparator',',')
dlmwrite('kf100CR6.m', Data)
clear all
mdata = load('kf100CR6.m');
size(mdata)
ans =
1248 4
mdata = [log(mdata(:,1)) log(mdata(:,2)) mdata(:,2) mdata(:,3) log(mdata(:,4))];
size(mdata)
ans =
1248 5

Connectez-vous pour commenter.


Arif Ahmed
Arif Ahmed le 6 Juil 2020
Modifié(e) : Walter Roberson le 6 Juil 2020
thanks for the reply...
the value that i got from the 'mdata(:,1)' does not match. please see the following example!!!
>> mdata(:,1)
ans =
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
-2.3026
.
.
.
.................
-------------------------------
and these are original data for the first column in the 'kf100CR6.m' or '100Cr6.csv' file:
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
.
.
.
.......

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by