Reading file After a special character delimiter in matlab - matlab

I have a file in which sentences ends like ./.
I want to read the file in a cell array, one cell for every line. Could you please tell me how to do that using textscan.
Basically I want to know how to put the delimiter ./.

well i am not sure if this is helpful or not
in the normal case of a new line for each sentence you could use
tline = fgetl(fileID);
D=textscan(tline,'%s','delimiter','./.');
but if your file doesn't have new lines for each sentence just ./. as a separator there are two cases that the sentences don't contain any characters used as a delimiter i.e . or /
in that case you can try something like
C = textscan(fileID,'%s %*1s','delimiter','/','MultipleDelimsAsOne',1);
the other case if your sentences did contain these characters then i think you can't use them as a delimiters but i might be wrong

Related

Reading data from .txt file into Matlab

I have been trying in vain for days to do one seemingly simple thing--I want to read data from a .txt file that looks like this:
0.221351321
0.151351321
0.235165165
8.2254546 E-7
into Matlab. I've been able to load the data in the .txt file as a column vector using the fscanf command, like so:
U=fscanf(FileID, '%e')
provided that I go through the file first and remove the space before the 'E' wherever scientific notation occurs in the data set.
Since I have to generate a large number of such sets, it would be impractical to have to do a search-and-replace for every .txt file.
Is there a way for matlab to read the data as it appears, as in the above example (with the space preceding 'E'), and put it into a column vector?
For anyone who knows PARI-GP, an alternate fix would be to have the output devoid of spaces in the first place--but so far I haven't found a way to erase the space before 'E' in scientific notation, and I can't predict if a number in scientific notation will appear or not in the data set.
Thank you!
Thank you all for your help, I have found a solution. There is a way to eliminate the space from PARI-GP, so that the output .txt file has no spaces to begin with. I had the output set to "prettymatrix". One needs to enter the following:
? \o{0}
to change the output to "Raw," which eliminates the space before the "E" in scientific notation.
Thanks again for your help.
A simple way, may not be the best, is to read line by line, remove the space and convert back to floating point number.
For example,
x = []
tline = fgetl(FileID);
while ischar(tline)
x = [x str2num(tline(find(~isspace(tline))))]
tline = fgetl(FileID);
end
One liner:
data = str2double(strsplit(strrep(fileread('filename.txt'),' ',''), '\n'));
strrep removes all the spaces, strsplit takes each line as a separate string, and str2double coverts the strings to numbers.

Import text file in MATLAB

I have a tab delimited text file with suffix .RAW.
How can I load the data from the file into a matrix in MATLAB?
I have found readtable, but it doesn't support files ending with suffix .RAW.
Do I really have to use fread, fscanf, etc. to simply load a text file into a matrix?
You can use the dlmread() function. It will read data from an ASCII text file into a matrix and let you define the delimiter yourself. The delimiter for tabs is '\t'.
>> M = dlmread('Data.raw', '\t')
M =
1 2 3
4 5 6
7 8 9
Just for your information there is also the tdfread() function but I do not recommend using it except in very specific cases. dlmread() is a much better option.
.RAW is a generic file extention. You should know the format of your RAW file (especially if your file contains a combination of numbers, data structures etc). If it is a simple text file with a single 2D table, you can easily read it with fscanf, fread, fgetl, fgets, etc
Here is a simple example for a 2D table (matrix):
Let's assume that each row of your table is separated by a carriage return from its following rows. We can read each row by fgetl() and then extract numbers using str2num().
fid=fopen('YourTextFile.RAW');
Data=[];
i = 0;
while 1
i = i + 1;
tline = fgetl(fid);
if ~ischar(tline), break, end
Data(i,:) = str2num(tline);
end
fclose(fid);
disp(Data)
For more complex data structure, the code should be changed.
For a 2D table (a special case) the above simple code can be easily exchanged by dlmread() function.

Matlab - read specifics of textfile and give specific output

I have a text file in Matlab that contains comment strings as well as variables and I am trying to figure out the best way to read this file and give an output as different variables that can easily be plugged into equations later on.
The text file looks something like this:
#Comments
2
#Comments
#Comments
1.1 2.55 4.32
1.9 2.76 8.95
1 3.65 9.12
I want an output so that each number is given a variable and the strings with the #s in front are ignored.
ex output:
i=2
a1=1.1
b1=2.55
c1=4.32
a2=1.9
b2=2.76
c2=8.95
a3=1
b3=3.65
c3=9.12
And these variables will be stored for later use. Thanks in advance to anyone who can help.
If you used textscan you can set the CommentStyle to # - this will ignore the lines starting with a #. Looking at your data, you should set your delimiter to a space. As some of your lines seem to be shorter than others you should probably set the EmptyValue parameter - this will replace any empty fields with a flag of your choosing, for example Inf or NaN, or just zero. The command will look something like this:
fid = fopen(filename)
data = textscan(fid, '%f%f%f', 'Delimiter', ',', 'CommentStyle', '#', 'EmptyValue', NaN)
This will put your data into a cell array - I am not sure how you could elegantly assign each value to a completely different variable.

MATLAB: textscan using width delimited txt file

I'm trying to import a width delimited txt file using the textscan function. The file is 80 characters wide, with no delimiter, and the desired resulting 12 columns are different widths of characters. I have tried to do this by specifying the width of the string, (i.e 12 strings, each of a different width of characters that add up to 80) but as soon as there is a space (because certain values are missing) MATLAB interprets this as my delimiter and messes up the format.
data= textscan(fileID, '%5s %7s %1s %1s %1s %17s %12s %12s %10s %5s %6s %3s');
I can work around this using Excel but this seems like a bad solution. Is there any way of doing this using MATLAB, maybe a different function than textscan/make textscan forget delimiters and just deal with width of the string?
You need to change the value of the delimiter and white space characters to empty:
format_string = '%5s %7s %1s %1s %1s %17s %12s %12s %10s %5s %6s %3s';
C = textscan(fid, format_string, 'delimiter', '', 'whitespace', '');
That way MATLAB will treat each character, including spaces, as valid characters.
Hmmm, I have experienced the same problem with textscan. Well, here is a long way around it (it is by no means the best solution, but it should work)
fid=fopen('txtfile.txt','rt'); %//load in file
a=fscanf(fid'%c'); %//scan the thing into chars
fclose(fid);
for r = 0:NumberOfRowsInUrData -1 %//Now the loop... Number of rows in your data can also be calculated by size(a,2)/20
b(r+1,:) = a(1+20*r:20*(r+1)); %// this will correctly index everything
end
The good thing is that now everything is in the matrix b, you can simply index your chars like string1 = b(:,1:5) and it will output everything in a nice matrix.
The downside ofc is the for loop, which I think you should be able to replace with something like a cellfun or something.

Exporting cell array with both text and numbers to csv file in Matlab

I have a cell array with nine columns (the first eight text and the ninth numbers) and thousands of rows that I would like to export to a csv file.
I have tried to follow the suggestions provided in similar questions and I take that the best way to proceed is to use the fprintf function:
fid = fopen(outputfile, 'w')
fprint(fid, ???, variable{:,:})
fclose(fid)
Nevertheless, I cannot figure out what I am supposed to write in the middle. I have tried several combinations using "%s", "\n", "\t", but it does not seem to work. Ideally, I would like to separate each column by either a ";", "," or a tab, and to make sure that the decimals of the values are not lost.