Matlab CSV parsing error?

조회 수: 10 (최근 30일)
Elie Younes
Elie Younes 2016년 5월 7일
편집: Stephen23 2016년 5월 7일
So let me preface this by saying, I am very new to Matlab.
Attached, I have an 11 x 8 CSV file. My script is supposed to take the phrases in the cells, turn them into a Matlab Variable, and place them in different aspects of my GUI.
The part of my script that takes the CSV and parses it into a variable is:
FileID = fopen('lifeacceleratedinteractions.csv' , 'r');
Contents = textscan(FileID, '%s%s%s%s%s%s%s%s' , 'Delimiter' , '"');
global Contents
assignin('base' , 'Contents' , Contents);
fclose(FileID);
The issue that arises from this is there are gaps, and information in the wrong spot. Stephen pointed out that there were commas all over the place, and to try using '%q' instead of '%s', but that did not work.
I'm rewording in hopes that I do a better job of explaining my situation. I have also included the CSV I'm working with, the .fig and the .m files as well. The lines that start to deal with the CSV is at 111.
  댓글 수: 2
Stephen23
Stephen23 2016년 5월 7일
편집: Stephen23 2016년 5월 7일
Note that it is much more reliable to pass variables rather than use assignin. Using assignin is slow and can have problems that are hard to debug. Passing variables properly is the neater, faster, and more reliable way to get variables from one workspace to another:
Elie Younes
Elie Younes 2016년 5월 7일
Those links helped alot. Someone else on here said to just use assignin and I'd be fine.

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2016년 5월 7일
편집: Stephen23 2016년 5월 7일
The issue is that some of the strings seem to contain commas, which you are also using as column delimiters. Luckily for you the CSV file also seems to have been formatted correctly with quotation marks, so one solution is to simply use %q instead of %s in your textscan format string, which will keep quoted strings together, as the documentation clearly explains:
'%q%q%q%q%q%q%q%q%q%q%q'
or even better:
N = number of columns in CSV file
fmt = repmat('%q',1,N);
C = textscan(FileID, fmt, 'Delimiter',',');
If that does not resolve the issue then you should edit your question and upload the file using the paperclip button.
Note: you write that the CSV file "is an 8 x 11 array of phrases", but you only define eight '%s' in your format string: something is not correct here, as the format string defines the columns, so should have 11 %s tokens. (remember "8 x 11" means eight rows and eleven columns).
  댓글 수: 5
Elie Younes
Elie Younes 2016년 5월 7일
편집: Elie Younes 2016년 5월 7일
I didn't mean to imply there were multiple definitions of matrices. I was just trying to say that I was wrong and mixed up my rows and columns.
Stephen23
Stephen23 2016년 5월 7일
편집: Stephen23 2016년 5월 7일
textscan works fine for me:
>> fmt = repmat('%q',1,8);
>> fid = fopen('lifeacceleratedinteractions.csv','rt');
>> C = textscan(fid,fmt,'Delimiter',',');
>> fclose(fid);
>> C = horzcat(C{:});
>> size(C) % check the size (rows x columns):
ans =
11 8
>> C{9,6} % show that textscan has kept quoted strings together:
ans =
Get gender specific toys, clothes, and accessories without knowing the baby's gender.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by