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

19 ビュー (過去 30 日間)
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 件のコメント
Renato Agurto
Renato Agurto 2015 年 12 月 22 日
編集済み: Renato Agurto 2015 年 12 月 22 日
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)?

サインインしてコメントする。

回答 (2 件)

Jos
Jos 2015 年 12 月 22 日
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 2015 年 12 月 22 日
編集済み: Benjamin Alvarez 2015 年 12 月 22 日
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 件のコメント
Hans Jakob
Hans Jakob 2015 年 12 月 22 日
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 2015 年 12 月 22 日
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)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by