Is there a way to recognize blank lines in Matlab? - 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

Related

Matlab: How can i read the value (string or number) of a variable?

Maybe the question is strange but anyway....
How can i read the value (string or number) of the variable number_of_plots or color? (i want to use the variable/array diagram options to solve this problem)
My Code:
diagramoptions = [];
wholecontent = fileread('aaa.txt')
sections = regexp(wholecontent, '\*+([^*]+)\*+([^*]+)', 'tokens')
for section = sections
switch(strtrim(section{1}{1}))
case 'Diagram Options' %Diagram Options -> siehe meine Gliederung im .txt file
keyvalues = regexp(section{1}{2}, '([^\n\r=]+)=([^\n\r=]+)', 'tokens')%\n -> new line; \r carriage return
diagramoptions = cell2table(vertcat(keyvalues{:}), 'VariableNames', {'Key', 'Value'})
otherwise
warning('Unknown section: %s', section{1}{1})
end
end
openvar diagramoptions
My Input "aaa.txt":
******************* Diagram Options****************
number_of_plots=4
header=Number of cycles
color=red
xlabel= RPM
ylabel= degree
Here's a naive way of doing it... It doesn't scale well and it does unnecessary work.. But it's something for you to build upon.
fileId = fopen('test.txt');
c = textscan(fileId, '%s', 'Delimiter', '=');
fclose(fileId);
for i = 1: length(c{1,1})
if (strcmp(c{1,1}{i,1}, 'number_of_plots'))
number_of_plots = c{1,1}{i+1,1};
elseif strcmp(c{1,1}{i,1}, 'color')
color = c{1,1}{i+1,1};
end
end
So, read in the file and delimit at = makes you know that any match on e.g. number_of_plots is in the next row. So just loop through and pick it out.
You can use the function eval in order to run a .txt file as it was a .m file:
fid = fopen('aaa.txt') %open the file
tline = fgetl(fid); %read the first line
while ischar(tline)
if ~isempty(strfind('tline','number_of_plots')) | ~isempty(strfind('tline','color='))
try %if it's possible matlab execute this line
eval(tline)
end
end
tline = fgetl(fid); %read the next line
end
fclose(fid)
But in this case you need to add some quotation marks to your aaa.txt so that matlab can create the variables:
******************* Diagram Options****************
number_of_plots=4
header='Number of cycles'
color='red'
xlabel='RPM'
ylabel='degree'

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.

How to use fscanf to detect line breaks?

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.

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