The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

MATLAB program bullshit, reading input files.

DemerdarDemerdar Registered User regular
edited September 2008 in Help / Advice Forum
Hey ya'll, I have a question about MATLAB. How do I read in a text file such as this:

3
1.0 2.0
3.0 4.0
5.0 6.0

Where the 3 is an integer describing how many coordinate points there are in the system. I need to read in the left column into a vector x, and the right column in to the vector y.
fid=fopen('input.txt','r');
m=fscanf(fid,'%d',1)
x=0;
y=0;
for i=1:m
    x(i) = fscanf(fid, '%g', 1)
    y(i) = fscanf(fid, '%g', 1)
end

Here is what I've written so far, but alas, it isn't working. Maybe I'm not reading how the fscanf function works. Any help would be appreciated (I know how to read input in C++, but MATLAB's syntax throws me for a loop every time).

Thanks.

y6GGs3o.gif
Demerdar on

Posts

  • bowenbowen Sup? Registered User regular
    edited September 2008
    You'll probably know a lot more about it than I do, but use this as a good reference:

    MATLAB file IO w/ scanf

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • DaenrisDaenris Registered User regular
    edited September 2008
    On a simple file like that you can use something like importdata.

    like a = importdata('myfile')
    and a.data should hold all your points. Then you can do x = a.data(:,1) and y = a.data(:,2) and you're done.

    Edit:

    Or, if your file format isn't fixed, get rid of the first line and just do a = load('myfile') and then x = a(:,1) and y = a(:,2) and you're done.

    If you absolutely have to use fscanf then
    fid=fopen('input.txt','r');
    m=fscanf(fid,'%d',1)
    for i=1:m
        temp = fscanf(fid,'%g %g',2);
        x(i) = temp(1);
        y(i) = temp(2);
    end
    fclose(fid);
    
    works.

    Daenris on
  • DemerdarDemerdar Registered User regular
    edited September 2008
    Daenris wrote: »
    fid=fopen('input.txt','r');
    m=fscanf(fid,'%d',1)
    for i=1:m
        temp = fscanf(fid,'%g %g',2);
        x(i) = temp(1);
        y(i) = temp(2);
    end
    fclose(fid);
    
    works.

    Ahh, I see how this wacky shit works now...couldn't I also just get rid of the 'temp' variable and do this:
    [x(i),y(i)] = fscanf(fid, '%g %g', 2);
    

    ?

    Demerdar on
    y6GGs3o.gif
  • DaenrisDaenris Registered User regular
    edited September 2008
    No, it won't work like that, because fscanf only returns a matrix as the first output, and the count of the number of read items as the second. But why bother with the loops? You don't need the loop at all. Either the importdata or load options would work better since Matlab is designed to work with matrices.

    Daenris on
  • DemerdarDemerdar Registered User regular
    edited September 2008
    Daenris wrote: »
    No, it won't work like that, because fscanf only returns a matrix as the first output, and the count of the number of read items as the second. But why bother with the loops? You don't need the loop at all. Either the importdata or load options would work better since Matlab is designed to work with matrices.

    Yeah... I don't know. I guess I can do away with the integer value at the beginning of the file... I'm sure my prof will approve.

    I..erm.. just HATE Matlab :(

    Demerdar on
    y6GGs3o.gif
  • DaenrisDaenris Registered User regular
    edited September 2008
    Demerdar wrote: »
    Daenris wrote: »
    No, it won't work like that, because fscanf only returns a matrix as the first output, and the count of the number of read items as the second. But why bother with the loops? You don't need the loop at all. Either the importdata or load options would work better since Matlab is designed to work with matrices.

    Yeah... I don't know. I guess I can do away with the integer value at the beginning of the file... I'm sure my prof will approve.

    I..erm.. just HATE Matlab :(

    Well, even if you don't get rid of it you can use the importdata function. You can then do a check of the size of your resulting matrices against the int value on the first line to ensure that the number of points matches the value, if you really need to. I'm just saying, that if there's not some requirement on the file format you can get rid of that line and use load. If there is a requirement, use importdata. Don't bother with the loops in this simple case.

    For more complicated files, looping and using fscanf (or textscan) is fine.

    Daenris on
Sign In or Register to comment.