How to use fscanf to detect line breaks? - matlab

I am using fscanf to import data into MATLAB. My text file is formatted as such:
######-10-K-########
######-10-K-########
The first number can vary anywhere from 6-9 characters. Right now I am using the code:
var = fcanf(fn, %s)
which is resulting in var being equal to one giant string.
I read that using %s will go on until it detects a whitespace. Is there anyone to get it to stop when it detects a linebreak instead?

Matlab has many functions, you can use textscan for example. Note that using textscan as follows trims all spaces in each line.
f = fopen('your_file_name');
file_data = textscan(f, '%s', 'Delimiter', '');
fclose(f);
Another example with fgetl as follows. fgetl keeps indents and spaces in each line.
lineCt = 1;
f = fopen('your_file_name');
tline = fgetl(f);
while ischar(tline)
f_data{lineCt} = tline;
lineCt = lineCt + 1;
tline = fgetl(f);
end
fclose(f);
Note that it should be fscanf instead of fcanf as you wrote.

Related

How to read a single character in file using MATLAB?

In my file data.txt, I have a string abcdefgh. Now I want to take just 1 character without read whole string. How can I do this in MATLAB?
For example, I want to take the first character, I use c = fscanf(data.txt, '%c'); and c = textscan(data.txt, '%c'); but it read whole line in data.txt. I know that c(1) is my answer but I don't want to do that.
You can limit the number of characters that are read in using the third input to either fscanf or textscan.
fid = fopen('data.txt', 'r');
c = fscanf(fid, '%c', 1);
c = textscan(fid, '%c', 1);
You could also just use a lower-level function such as fread to do this.
fid = fopen('data.txt', 'r');
c = fread(fid, 1, '*char');

Matlab - string containing a number and equal sign

I have a data file that contains parameter names and values with an equal sign in between them. It's like this:
A = 1234
B = 1353.335
C =
D = 1
There is always one space before and after the equal sign. The problem is some variables don't have values assigned to them like "C" above and I need to weed them out.
I want to read the data file (text) into a cell and just remove the lines with those invalid statements or just create a new data file without them.
Whichever is easier, but I will eventually read the file into a cell with textscan command.
The values (numbers) will be treated as double precision.
Please, help.
Thank you,
Eric
Try this:
fid = fopen('file.txt'); %// open file
x = textscan(fid, '%s', 'delimiter', '\n'); %// or '\r'. Read each line into a cell
fclose(fid); %// close file
x = x{1}; %// each cell of x contains a line of the file
ind = ~cellfun(#isempty, regexp(x, '=\s[\d\.]+$')); %// desired lines: space, numbers, end
x = x(ind); %// keep only those lines
If you just want to get the variables, and reject lines that do not have any character, this might work (the data.txt is just a txt generated by the example of data you have given):
fid = fopen('data.txt');
tline = fgets(fid);
while ischar(tline)
tmp = cell2mat(regexp(tline,'\=(.*)','match'));
b=str2double(tmp(2:end));
if ~isnan(b)
disp(b)
end
tline = fgets(fid);
end
fclose(fid);
I am reading the txt file line by line, and using general expressions to get rid of useless chars, and then converting to double the value read.

Can Matlab readtable work on a text file delimited with variable numbers of spaces?

I have several text files that are formatted something like this, each file with a different number of rows (but around 1000 rows in each).
Id X Y Curve
1 0.0000000000 -0.0000286102 Domain_BCs
2 0.0010000000 -202.5294952393 Domain_BCs
3 0.2028919513 -1098.9577636719 Domain_BCs
4 1.0000000000 -2286.1757812500 Domain_BCs
I want to bring this data into Matlab, break it into separate vectors according to the string in the Curve column, and plot Y as a function of X.
The data is space-delimited with a variable number of spaces, and there are also a variable number of spaces at the start of each row (before the Id column). I know that readtable would work if there were no spaces at the beginning of the rows and only one space between columns. Is there any way to make readtable work with data in this format?
I also considered using textscan, but my understanding is that I would need to know the number of rows in order to use textscan, which makes things trickier because the number of rows is different for each file I want to process.
Textscan is exactly meant for this purpose. You can just use textscan without knowing the number of lines :) Any amount of whitespace is interpreted as a single delimiter standard. So just use:
FID = fopen('test2.txt');
formatSpec = '%d %f %f %s';
C = textscan(FID,formatSpec);
fclose(FID)
In test2.txt I just pasted your example a few times (without headers).
Each column of your file is then read into a cell in C.
Soruce: http://www.mathworks.nl/help/matlab/ref/textscan.html
fgets - Read lines without concerning number of lines
strsplit - split a string with delimiters
fid = fopen('yourfile.txt');
tline = fgets(fid);
while ischar(tline)
trow = strsplit(tline, ' ', 'CollapseDelimiters',true);
tline = fgets(fid);
end
fclose(fid);
If you want to speed up a little bit,
fid = fopen('yourfile.txt');
counter = 0;
tline = fgets(fid);
trow = strsplit(tline, ' ', 'CollapseDelimiters',true);
while ischar(tline)
counter = counter + 1;
tline = fgets(fid);
end
T = zeros(counter, length(trow));
frewind(fid);
while ischar(tline)
trow = strsplit(tline, ' ', 'CollapseDelimiters',true);
tline = fgets(fid);
end
fclose(fid);

Using matlab to read textfile but then skip lines with a # sign at the beginning of them

function [input]= read_input()
fid= fopen ('input.txt');
tline=fgets(fid);
while ischar(tline)
if tline =='#'
end
tline = fgets(fid);
end
fclose(fid)
This is my code so far I'm trying to read a file and then just take the numerically values while skipping the lines that start with a #.
Thanks for any help in advance.
I recommend a different approach.
Take advantage of the built-in functionality MATLAB provides, and use textscan:
fid = fopen('input.txt');
C = textscan(fid, '%s', 'Delimiter', '\n', 'CommentStyle', '#');
C = C{:};
fclose(fid);
After this, you'll end up with a cell array C that contains all the lines from your input file that don't start with a hash sign.
Update the if
if tline(1) =='#'
continue;
end

Is there a way to recognize blank lines in Matlab?

Is there a way to recognize blank lines when you are scanning a text file in Matlab? I want to parse the files based on the blank lines in between the text. Is this possible?
Yes, it's possible. A MATLAB snippet would look something like:
fid = fopen('reader.m');
newline = sprintf('\r\n');
line = fgets(fid);
while ischar(line)
if strcmp(newline, line)
disp('Empty line');
else
disp('Non-empty line');
end
line = fgets(fid);
end
Here's one possibility:
fid = fopen('myfile.txt');
lines = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
lines = lines{1};
% lines now contains a cell array of strings,
% one per line in the file.
% Find all the blank lines using cellfun:
blank_lines = find(cellfun('isempty', lines));
without \r...now works fine
fid = fopen('reader.m');
newline = sprintf('\n');
line = fgets(fid);
while ischar(line)
if strcmp(newline, line)
disp('Empty line');
else
disp('Non-empty line');
end
line = fgets(fid);
end