Reading every 10th Trial Error

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');

Answers (2)

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 Comments

When I change i=1 the script still gives the same error. So I also made the f=1 and the count=1.
Did you change also for i = 0:10;
Yes i have also changed that.
Then, you're not showing all the relevant code.
the only other thing that is used is a function I created called openfile:
function [fid,name]=openfile ()
name=input('Please enter the name of the file:' ,'s');
fname=[name,'.txt'];
fid = fopen(fname,'rt');
if fid < 0
error('file does NOT exist');
else
display ('opening file..... ')
end
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).
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.

Sign in to comment.

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 Comments

I count to 10 because I want the script to read only the 10th trial then to continue reading the text file and read the 20th, 30th, 40th,... trial. There is a total of 200 Trials so the counter would only have to count to 20. Nothing should happen when the 11th, 12th, 13th,eetc. values. dummy is the array I want the 10th, 20th, 30th,etc trials to go into. The file will always have the proper number of values.
Have you fixed it, do you want us to fix it, or do you want more hints?
If you are able to fix it that would be great!
Okay, done. Enjoy the read.
Thanks a lot Geoff but I am still receiving an error when using the paradigm you made based on mine:
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 );
Error is as follows:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Tenth_trial_trial (line 7)
dummy(k) = fscanf(fid, '%d', 1);
Also what if each tenth trial had a header such as "Double" how would I incorporate that into the script?
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.
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.

Sign in to comment.

Categories

Asked:

on 20 Mar 2012

Community Treasure Hunt

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

Start Hunting!