How can i read a csv-file with strings and integers into script and then write new values in some cells at the end

30 Ansichten (letzte 30 Tage)
I have a csv file which I want to read into a script. The file consists of 6 rows and 3 columns. The first 2 columns are strings while column 3 is integers.
[hansjv, Hjv45907, 1613
heidi, uTsira15, 1477
gunnar, Grati5, 1388
peter, Sl3mm3stad, 1586
elisabeth, Jer3n, 1720
petter, vold23, 1609]
My goal is to load these values from a csv file into the script. After this is done I'm going to do some calculations with the integers in column 3. When this is completed I wish to set these values into spesific cells in the third column.
Hope someone can help me with this problem. Thank you :)
  1 Kommentar
Renato Agurto
Renato Agurto am 22 Dez. 2015
Bearbeitet: Renato Agurto am 22 Dez. 2015
Hi. How is the csv file fomatted? with commas (,) or semicolons (;) ? Or is it just like in your question (also with the '[' and '[' in the 1st and last row)?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jos
Jos am 22 Dez. 2015
Hi,
if your file containing the information is called 'test.csv' you can read in everything using:
p = readtable('test.csv','ReadVariableNames',false);
you can then use or change values in the last column, for instance the 4th one down, by
p.Var3(4) = 1500;
Everything is saved in the single variable p
Hope that helps

Benjamin Alvarez
Benjamin Alvarez am 22 Dez. 2015
Bearbeitet: Benjamin Alvarez am 22 Dez. 2015
Hi,
You can do it directly using xlsread command : [numeric, text, matrix] = xlsread('Test.csv');
You can do some computation on the last column of matrix : matrix{2,3} = matrix{4,3} - 155 % As example
Then you can save/update your csv file by using : xlswrite('Test.csv', matrix)
  2 Kommentare
Hans Jakob
Hans Jakob am 22 Dez. 2015
I am unable to open the csv file with xlsread. In addition, I'm using a Mac. The xlswrite-function isn't working. That means that I am unable to use the xls-functions, both read and write.
Originally I wrote my script for a xl-file, but am now trying to change to csv-file. The problem is as mentioned above, that the file consists of both strings and integers.
Jos
Jos am 22 Dez. 2015
as in the answer above, readtable will import xls and csv files containing different types of data. The writetable command will write the entire table to file as shown below
writetable(p,'updated_test.csv','WriteVariableNames',0)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by