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!