fopen files in one dataset

조회 수: 2 (최근 30일)
TS
TS 2020년 12월 14일
댓글: Mathieu NOE 2020년 12월 14일
Hi!
I collected two different data sets from 30 subjects, so in total I have 60 different .txt files. My goal is to have them all in one big dataset, already in order. The files have different endings (_MT or _OT), depending on the coniditon. Each subject should therefore appear twice within the dataset, but already labeled with 0 or 1 for the condition. I tried to go with a for loop.
for ssubj = 001:030
filename = ['VP0', num2str(ssubj), '_MT.txt'];
filename2 = ['VP0', num2str(ssubj), '_OT.txt'];
file = fopen('/Users/cf/Documents/MATLAB/Passive');
tmp = textscan(file, '%s%f%f%f', 'CollectOutput', true, 'Delimiter', ' ', 'HeaderLines', 3);
[~] = fclose(file);
dat(: , 1, ssubj) =
end
I always get this error... Or is the directory already wrong?
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Thank you for your help!
  댓글 수: 5
Rik
Rik 2020년 12월 14일
Are you sure the files exist? You don't check if they exist.
You can check if Matlab is able to open the file by checking if the fid is above 0.
Mathieu NOE
Mathieu NOE 2020년 12월 14일
you can , at least, test my code in the same directory of your data files
it should work , because this is very basically what I did
then, if you need to access your data from different folder(s) use fullfile with the folder / directory info

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

채택된 답변

Rik
Rik 2020년 12월 14일
You don't use either filename when calling fopen. How is Matlab supposed to know which file you mean?
I would suggest using either readlines (introduced in R2020b) or readfile (which you from the FEX, if you are using R2017a or later, you can also get it through the AddOn-manager). Both of those functions will be able to deal with the UTF-8 encoding in your files. The first will return your file as a string vector, the latter as a cellstr.
  댓글 수: 2
TS
TS 2020년 12월 14일
Hey, sorry I dont quite get that. So instead of fopen(filename) I use the code of readlines?
Rik
Rik 2020년 12월 14일
%instead of this
filename = ['VP0', num2str(ssubj), '_MT.txt'];
fid = fopen(filename);
out1 = textscan(fid, '%s%f%f%f', 'CollectOutput', true, 'Delimiter', ' ', 'HeaderLines', 3);
fclose(fid);
%use this
filename = ['VP0', num2str(ssubj), '_MT.txt'];
out1=readfile(filename); %(requires downloading the FEX submission or adding it through the Add-On Manager)
%or this
filename = ['VP0', num2str(ssubj), '_MT.txt'];
out1=cellstr(readlines(filename));%(requires R2020b)

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

추가 답변 (0개)

카테고리

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