Textscan difficulties opening file.

Trying to read a file but failing miserably. File that I'm trying to open is attached. Here's what I'm doing:
fnm = '0782en_OD_monoc_9_4_08.csv.analized'
fid = fopen(fnm,'r')
textData = textscan(fid,'%s%s%s%s%s%s%s%s%s%f%f','delimiter','\t');
When opened on the occasion that it works, it comes up with a 1x11 cell with none of the data inside because the s's and f's inside of the percent symbols are likely off of what they should be. If someone could point me in the right direction for what I should put in the percents/ give me a script to open this file, that would be much appreciated. I highly recommend trying to open the attached file yourself to see if there are other issues.
Thanks all.
Edit: I'd be most appreciative if someone were to just dump the entire list of commands that I should perform into the comments. Would make it way easier for a noob like me to follow.

 Accepted Answer

Stephen23
Stephen23 on 10 Aug 2018
Edited: Stephen23 on 10 Aug 2018
Something like this will get you started:
opt = {'Delimiter','\t','HeaderLines',1,'CollectOutput',true};
fmt = '%s%f%f%f%f%f%f%f%f%f%f%f%f%s';
[fid,msg] = fopen('0782en_OD_monoc_9_4_08_csv_analized.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
% Remove NaN rows:
idx = ~all(isnan(C{2}),2);
pth = C{1}(idx,:);
mat = C{2}(idx,:);
rgb = C{3}(idx,:);
Imports this data:
>> pth
pth =
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
... lots of lines
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
'C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv'
>> mat
mat =
2 800 600 1668 1242 1000 0 0 14 -3 0 0
2 800 600 1668 1242 1000 0 0 104 -75 0 0
2 800 600 1668 1242 1000 0 0 55 -59 0 0
2 800 600 1668 1242 1000 0 0 158 1 0 0
2 800 600 1668 1242 1000 0 0 163 -47 0 0
2 800 600 1668 1242 1000 0 0 160 43 0 0
... lots of lines
2 800 600 1668 1242 1000 0 0 21 -12 0 0
2 800 600 1668 1242 1000 0 0 17 -1 0 0
2 800 600 1668 1242 1000 0 0 18 5 0 0
>> rgb
rgb =
'java.awt.Color[r=255,g=0,b=0]'
'java.awt.Color[r=255,g=0,b=0]'
'java.awt.Color[r=255,g=0,b=0]'
'java.awt.Color[r=255,g=0,b=0]'
... lots of lines
'java.awt.Color[r=0,g=0,b=255]'
'java.awt.Color[r=0,g=0,b=255]'
'java.awt.Color[r=0,g=0,b=255]'
'java.awt.Color[r=0,g=0,b=255]'
'java.awt.Color[r=0,g=0,b=255]'

4 Comments

copy & pasted; all workspace variables are there, for whatever reason though, C = textscan(fid,fmt,opt{:}); gives the error message "??? Error using ==> textscan Invalid file identifier. Use fopen to generate a valid file identifier."
Does this mean fid didn't get assigned to the right file?
@ethan Campbell: it means that fopen could not open the file. Usually this is because the file path is not correct, or because of a spelling mistake. Check the name carefully. If the file is not located in the current directory then you will need to provide the full filename, e.g.:
dpt = 'path to the directory where the file is saved';
fnm = '0782en_OD_monoc_9_4_08_csv_analized.txt';
[fid,msg] = fopen(fullfile(dpt,fnm),'rt');
... etc
This is why you should always get the second fopen output msg and use it in the assert statement, just as my answer shows: it prints the reason for fopen not opening the file, which is a huge help when debugging.
even when I take the full path,
C:\Users\KumarN\Desktop\VisualFieldAnalysis\data\subjects\0782en\Visual Fields\DVF\DVF 9_4_08\0782en_OD_monoc_9_4_08.csv
and assign it to dpt in the fopen function, the msg variable comes out with a value of 'No such file or directory'... could this be a problem with the code or is it likely an issue with where the file is originally located?
Stephen23
Stephen23 on 10 Aug 2018
Edited: Stephen23 on 10 Aug 2018
"even when I take the full path ... and assign it to dpt in the fopen function, the msg variable comes out with a value of 'No such file or directory'"
The dpt variable should not include the filename, like you are doing. It should only include the path itself, like my answer shows. The filename is stored in the variable fnm.
To be honest, there is something strange with the file path: why is the same path stored in the first column of the file itself? This is highly unusual.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Aug 2018

Edited:

on 10 Aug 2018

Community Treasure Hunt

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

Start Hunting!