Solving Matlab fscanf & fgets Issue - Tips & Suggestions

  • MATLAB
  • Thread starter justinplume
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses using fgets to get the number of lines in a file and then using fscanf to get the data. However, there seems to be an issue with this approach as the handle is at the end of the file after using fgets, making it unable to read any data. The suggested solution is to go back to the beginning of the file or to close and re-open it. The conversation ends with the acknowledgement that the issue has been resolved by moving the fseek and mentioning that the initial approach was based on someone else's code.
  • #1
justinplume
2
0
i use fgets to get the number of lines in my file to get rid of the header, but when i do this i can't us fscanf to get the actual data, but it doesn't work any more. Tips and suggestions? Before i entered number of lines in the file. This is how it currently stands:

cldden = fopen(filename, 'r');
%fseek(cldden, 311, 'bof');
nLines = 0;
while (fgets(cldden) ~= -1),
nLines = nLines + 1;
end
length_of_data = nLines-7;
CLD_data_matrix_temp = fscanf(cldden, '%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg', [11,length_of_data]);
fclose(cldden);
 
Physics news on Phys.org
  • #2
I don't know matlab, but because of the function naming I will assume it works the same as in C++.
After fgets'ing all the data, your handle points to the end of the file, hence fscanf doesn't read anything anymore. You will have to go back to the beginning (isn't that what the commented out line does? otherwise just close and re-open the file).
 
  • #3
Thanks! i moved the fseek. I am working w/someone elses code and they had to enter the number of lines in a file and i thought that was silly and tried to update it. This worked!
 

1. How do I fix a fscanf error in Matlab?

The most common cause of a fscanf error is incorrect formatting of the input data. Make sure that the format specifier in the fscanf function matches the format of the input file. You may also need to use the strtrim function to remove any extra spaces from the input data.

2. Why is my fscanf function not reading all the data from my file?

The fscanf function reads data until it reaches a delimiter or reaches the end of the file. If your file contains extra data or does not have a delimiter at the end, the fscanf function may not read all the data. Make sure that your input file is properly formatted and contains the correct number of delimiters.

3. How do I read multiple lines of data using fscanf in Matlab?

The fscanf function reads data one line at a time. To read multiple lines, you can use a loop and store each line in a cell array or matrix. Alternatively, you can use the textscan function with the 'Delimiter' option set to a newline character to read multiple lines at once.

4. What is the difference between fscanf and fgets in Matlab?

The fscanf function reads data from a file according to a specified format, while the fgets function reads data as a string up to a specified delimiter. Additionally, fscanf can only read one line at a time, while fgets can read multiple lines.

5. How can I improve the performance of my fscanf or fgets function in Matlab?

One way to improve performance is to specify the size of the data that will be read in advance. This allows Matlab to allocate memory for the data, which can improve the speed of the function. You can also use the textscan function with the 'CollectOutput' option to read data into a matrix, which can be faster than using a loop with fscanf or fgets.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
1
Views
3K
Back
Top