How can i read a csv-file with strings and integers into script and then write new values in some cells at the end
18 次查看(过去 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
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
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
0 个评论
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 个评论
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 Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!