Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reading from a TXT file

Subject: Reading from a TXT file

From: Chiemera

Date: 27 Jun, 2011 16:06:56

Message: 1 of 3

Hello,
I want to read integer numbers from a txt file. My data:
a b err
3.1 1.563 0.34
3 1.552 0.32
3.7 1.54 0.3
.. .. ..
My code:
fid = fopen('veriTXT.txt');
k = 0; inp1=[]; inp2=[]; outp=[];
while (~feof(fid))
    k = k + 1;
    line = fgetl(fid);
    if ~ischar(line), break, end
    inp1(k,:) = fscanf(fid,'%d',1);
    inp2(k,:) = fscanf(fid,'%d',1);
    outp(k,:) = fscanf(fid,'%d',1);
end

But this code produces this error:
??? Subscripted assignment dimension mismatch.
Error in ==> deneme3 at 14
    outp(k,:) = fscanf(fid,'%d',1);

Where is the problem?
Thanks in advance.

Subject: Reading from a TXT file

From: Florin Neacsu

Date: 27 Jun, 2011 17:54:04

Message: 2 of 3

Chiemera <muratgok@gmail.com> wrote in message <82ce375b-13e3-4497-a4fe-4b3f9c07e56a@d22g2000yqn.googlegroups.com>...
> Hello,
> I want to read integer numbers from a txt file. My data:
> a b err
> 3.1 1.563 0.34
> 3 1.552 0.32
> 3.7 1.54 0.3
> .. .. ..
> My code:
> fid = fopen('veriTXT.txt');
> k = 0; inp1=[]; inp2=[]; outp=[];
> while (~feof(fid))
> k = k + 1;
> line = fgetl(fid);
> if ~ischar(line), break, end
> inp1(k,:) = fscanf(fid,'%d',1);
> inp2(k,:) = fscanf(fid,'%d',1);
> outp(k,:) = fscanf(fid,'%d',1);
> end
>
> But this code produces this error:
> ??? Subscripted assignment dimension mismatch.
> Error in ==> deneme3 at 14
> outp(k,:) = fscanf(fid,'%d',1);
>
> Where is the problem?
> Thanks in advance.

Hello,

I recommend you use the debugger. Insert a break point in the beginning of your code and then step through and see what happens.

My guess is that fgetl is the problem here. You read a line then go through that line with fscanf. You probably never go to the next line so next time fgetl is called is going to return something you don't expect. Read the doc for fgetl.
Do you actually need it?

Regards,
Florin

Subject: Reading from a TXT file

From: TideMan

Date: 28 Jun, 2011 08:11:34

Message: 3 of 3

On 28/06/2011 4:06 a.m., Chiemera wrote:
> Hello,
> I want to read integer numbers from a txt file. My data:
> a b err
> 3.1 1.563 0.34
> 3 1.552 0.32
> 3.7 1.54 0.3
> .. .. ..
> My code:
> fid = fopen('veriTXT.txt');
> k = 0; inp1=[]; inp2=[]; outp=[];
> while (~feof(fid))
> k = k + 1;
> line = fgetl(fid);
> if ~ischar(line), break, end
> inp1(k,:) = fscanf(fid,'%d',1);
> inp2(k,:) = fscanf(fid,'%d',1);
> outp(k,:) = fscanf(fid,'%d',1);
> end
>
> But this code produces this error:
> ??? Subscripted assignment dimension mismatch.
> Error in ==> deneme3 at 14
> outp(k,:) = fscanf(fid,'%d',1);
>
> Where is the problem?
> Thanks in advance.
Well, I wouldn't do it that way.
Try this:
c=textscan(fid,'%f%f%f','headerlines',1);
Now column 1 is in c{1}, column 2 is in c{2}, etc

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us