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.
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).
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.
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.
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.
Posts
MATLAB file IO w/ scanf
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
works.
Ahh, I see how this wacky shit works now...couldn't I also just get rid of the 'temp' variable and do this:
?
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.