Reading every 10th Trial Error
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have written some code to read the tenth trial of a text file. So the script can read the file with no problem but i keep getting an error some where in my for loop. please help:
count = 0;
f = 0;
i = 0;
for k = 0:20;
for i = 0:10;
fid = openfile;
dummy(i) = fscanf (fid,'%d');
count = 1 + count;
if count == 10;
f = f+1;
frandy(f) = dummy;
end
end
end
this is the error:
Subscript indices must either be real positive
integers or logicals.
Error in Tenth_trial_trial (line 7)
dummy(i) = fscanf (fid,'%d');
0 commentaires
Réponses (2)
Oleg Komarov
le 20 Mar 2012
You initialize i = 0. MATLAB convention for positions is to start from 1, i.e. there's no position 0 in any array.
Specifically, you cannot assign to
dummy(i) = fscanf (fid,'%d');
because at first run
dummy(0) = fscanf (fid,'%d');
7 commentaires
Oleg Komarov
le 21 Mar 2012
The error is caused by
for i = 0:10
it should be
for i = 1:11 %or 10
Nothing else in your script could mess it up.
Why am I saying so?
Because it points to "i" (so k = 0 is not involved, as you confirm with your previous comment).
Geoff
le 22 Mar 2012
I worked it out. MatLab's version of fscanf() doesn't behave like any other implementation of fscanf. So it's guaranteed to be a hitch for any C programmer. You think you're asking for one value, but unless you explicitly state that, you'll get every value in the file.
Geoff
le 20 Mar 2012
Your script has multiple errors, and some things that simply don't make sense. For example, why do you use i to count to 10, and then create a counter to do the same thing? What happens when you read the 11th, (12th, 13th, etc) values? Why is dummy an array? What happens if your file doesn't contain the expected number of values?
Once you have gone through the process of nutting it out and getting it going (which will be a very good exercise for you), consider the following:
vals = textread('yourfile.txt', '%d', 'delimiter', '');
frandy = vals(10:10:end);
Edit: fix for your code as requested
First, what's wrong (or unnecessary) with your program...
count = 0; % <-- You shouldn't even need 'count'.
f = 0; % <-- You shouldn't need this either.
i = 0; % <-- This is unused
for k = 0:20; % <-- Loops 21 times, not 20
for i = 1:10; % <-- Use anything but 'i'
fid = openfile; % <-- This is in the wrong place
dummy(i) = fscanf (fid,'%d'); % <-- Don't need to store in array
count = 1 + count;
if count == 10; % <-- You never reset count
f = f+1;
frandy(f) = dummy; % <-- Assigning array instead of value
end
end
end
% <-- You never close the file
Now, I didn't realise this until just now, but it turns out that fscanf doesn't behave like it's C counterpart. I have no idea why that should be, but look it up! fscanf(fid, '%d') doesn't just return one integer. It returns all of them! I think that is why you get the error. Because you're not assigning a scalar to dummy(i) - you're assigning an array.
So in fact, you could do this:
fid = openfile;
allvals = fscanf(fid, '%d', 200); % Limit to 200 values
dummy = allvals(10:10:end);
fclose(fid);
But that would be almost the same as my version using textread().
To follow your paradigm, you would do this:
fid = openfile;
dummy = zeros(1,20);
for k = 1:20
for n = 1:9
fscanf( fid, '%d', 1 );
end
dummy(k) = fscanf( fid, '%d', 1 );
end
fclose( fid );
Obviously, you don't need that inner loop, because you can do this:
fscanf( fid, '%d', 9 );
dummy(k) = fscanf( fid, '%d', 1 );
Or indeed:
vals = fscanf( fid, '%d', 10 );
dummy(k) = vals(10);
But then, we've already seen you don't really need any of the loops.
PS: Instead of your openfile function, why not use MatLab's uigetfile?
8 commentaires
Oleg Komarov
le 25 Mar 2012
If I understand corectly you want to import a file. What's the format of the file, i.e. what does it contain (numbers, how many columns), does it have a header, how often "double" repeats etc...
so that we could help you import it.
Geoff
le 25 Mar 2012
Chances are that the next value in the file is not an integer, and your function is attempting to assign the empty set to dummy(k). You are starting to stray from the original question and specification of the problem. As far as I am aware, I have solved your problem. If you're now changing the problem, accept the answer and create a new question. Also, I suggest you read the help for fscanf from beginning to end.
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!