필터 지우기
필터 지우기

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
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by