I can't read my .txt file using importdata. Problem with format?
Afficher commentaires plus anciens
Hello,
I made a script that reads a .txt file using textscan to manipulate some of the values, the outcome is another .txt file in the same format. Somehow, Matlab cannot read it, even though I produced it through the script.
These are my two files.

I don't know if this is relevant, but the parts that read the inputs and store the changed variables in my code are in these two pieces.
%for the input
A=importdata('subject 3.txt');
Only_Data=cell(size(A(27:end)));
[a,b]=size(A(27:end));
C=A(27:end);
y={A(1:26)};
For the outcome:
new_subject3=[new_subject3];
outfile='backMatch_VS_1BM_1412015_NS-003-1-updated';
filename=[outfile,'.txt'];
fid=fopen(filename,'w');
yNew=y{1};
fprintf(fid,'%s\n',yNew{:}); %this line prints the headers exactly as they were.
%we need this loop for a exact format
[nrows,ncols] = size(new_subject3);
for row = 2:nrows
if row<11
fprintf(fid ,'%s %7s %13s %13s %18s %13s %8s %12s %12s %6s %4s %7s\n', new_subject3{row}{1},new_subject3{row}{2},new_subject3{row}{3},new_subject3{row}{4},new_subject3{row}{5},new_subject3{row}{6},new_subject3{row}{7},new_subject3{row}{8},new_subject3{row}{9},new_subject3{row}{10},new_subject3{row}{11}, new_subject3{row}{12});
elseif (row<=100)&&(row>=11)
fprintf(fid ,'%s %6s %13s %13s %18s %13s %8s %12s %12s %6s %4s %7s\n', new_subject3{row}{1},new_subject3{row}{2},new_subject3{row}{3},new_subject3{row}{4},new_subject3{row}{5},new_subject3{row}{6},new_subject3{row}{7},new_subject3{row}{8},new_subject3{row}{9},new_subject3{row}{10},new_subject3{row}{11}, new_subject3{row}{12});
elseif (row<=nrows)
fprintf(fid ,'%s %5s %13s %13s %18s %13s %8s %12s %12s %6s %4s %7s\n', new_subject3{row}{1},new_subject3{row}{2},new_subject3{row}{3},new_subject3{row}{4},new_subject3{row}{5},new_subject3{row}{6},new_subject3{row}{7},new_subject3{row}{8},new_subject3{row}{9},new_subject3{row}{10},new_subject3{row}{11}, new_subject3{row}{12});
end
end
fclose(fid);
When I try to read my outcome, I can't do it.
A_NEW=importdata('backMatch_VS_1BM_1412015_NS-003-1-updated.txt');
Error using importdata (line 225)
Unable to load file.
Use TEXTSCAN or FREAD for more complex formats.
Caused by:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
According to Notepad++, my original file is shorter (59807) than my new one (67157).
Any clues on this? Is there a way to change my file to an acceptable format by matlab? How can I work this around?
Thanks in advance.
3 commentaires
per isakson
le 11 Août 2016
Why different encoding?


 
It's easier to help if you upload a text-file so that we can try it.
"importdata (line 225)"   doesn't make sense with R2016a. Which release do you use?
dpb
le 11 Août 2016
Well, as Per says, we can't test what we don't have but the error message is a klew...
Error using importdata (line 225)
Unable to load file.
...
Caused by:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
importdata has to infer the columns in the file from the data it finds; the above says it read some, then some more and discovered that vertcat didn't work. Since it's vertcat, that implies there weren't the same number of columns found on the second try as were determined to be thought to be in the file at first.
I've never actually used importdata so I don't really know it, but I'd be checking to see if there are actually the same number of columns written every row in the data portion, the number of delimiters matches in any blank rows that aren't totally blank, etc., etc., etc., ...
The file somehow isn't as regular as you think and we can only see what it looks like in Notepad which is notoriously bad for trying to see what's actually in a file...
Ajpaezm
le 12 Août 2016
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Text Data Preparation dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!