Read file names from .txt file in MATLAB - matlab

I am attempting to read in multiple file names from a .txt file. Each file names has multiple spaces and ends in different file formats.
When I try this code
M = textread('playlist.m3u', '%s')
I get the results to be the first string in the first row followed by the next string after the space is the next row ect.
One of the file names in the text file is "C:\Users\user\Music\Pink Floyd\Wish You Were Here (Matersound Gold Limited Edition)\03 - Have a Cigar.flac"
'C:\Users\user\Music\Pink'
'Floyd\Wish'
'You'
'Were'
'Here'
'(Matersound'
'Gold'
'Limited'
'Edition)\03'
'-'
'Have'
'a'
'Cigar.flac'
How do I simply read in all the files with each file taking up 1 cell in an cell array?

Use textscan and specify newline \n as the delimiter:
fid = fopen('playlist.m3u');
M = textscan(fid, '%s', 'delimiter', '\n')

Related

How to skip first few rows when reading from a file in matlab [duplicate]

When I try to use headerlines with textscan to skip the first line of the text file, all of my data cells are stored as empty.
fid = fopen('RYGB.txt');
A = textscan(fid, '%s %s %s %f', 'HeaderLines', '1');
fclose(fid);
This code gives
1x4 Cell
[] [] [] []
Without the headerlines part and without a first line that needs to be skipped in the text file, the data is read in with no problem. It creates a 1x4 cell with data cells containing all of the information from the text file in columns.
What can I do to to skip the first line of the text file and read my data in normally?
Thanks
I think your problem is that you have specified a string instead of an integer value for HeaderLines. The character '1' is interpreted as its ASCII value, 0x31 (49 decimal), so the first 49 lines are skipped. Your file probably contains 49 lines or less, so everything ends up being discarded. This is why you're getting empty cells.
The solution is to replace '1' with 1 (i.e. remove the quotes), like so:
A = textscan(fid, '%s %s %s %f', 'HeaderLines', 1);
and this should do the trick.

How can I write out header names along with data using dlmwrite?

I use this code to write out to a csv file inside a loop. The loop will run thousands of times and store these 5 data points after every run.
dlmwrite('test.csv', [outlet_info.outlet_row, outlet_info.outlet_col,
outlet_info.rel_err, outlet_info.new_est_darea, delta1)
How can I put header names in the csv file as well? eg. row, col, err, area, del
How can I append 5 new data as a new row after every loop? How do I use append?
Use fprintf to write the header as a single line into the file.
Then use dlmwrite to append data to it:
filename = 'test.csv';
fid = fopen(filename, 'w');
fprintf(fid, 'HEADER LINE HERE...\n');
fclose(fid)
for i = ... %(loop over your data here...)
dlmwrite(filename, [outlet_info.outlet_row, outlet_info.outlet_col,
outlet_info.rel_err, outlet_info.new_est_darea, delta1], '-append', 'precision', '%.6f', 'delimiter', ';');
end
You can also do this with using dlmwrite only. Just write the header first an then use the -append flag to append to it:
%prepare your header string
hdr = 'row, col, err, area, del';
%write header first
dlmwrite('test.csv', hdr, '');
%append your data to the same file
dlmwrite('test.csv', [outlet_info.outlet_row, outlet_info.outlet_col,
outlet_info.rel_err, outlet_info.new_est_darea, delta1, '-append');

Insert String in a CSV file matlab

how do I insert a string into a csv file in matlab. i used this code to write some data and create my csv file:
and here is the output of the code:
I'm trying to insert some text in the first 2 columns before the numerical data..
thanks in advance :)
There are several approaches are possible here.
Let's take a look at some of them:
If you need to add string to your csv file.
For example, I create some csv file like your:
q = [1 2 3 4 5 6 7];
csvwrite('csvlist4.csv',q,2,0);
All troubles is to add some string to csv - it's because we need to combine numeric and text data. There are no good functions for it in Matlab except low levels:
c = 'some big text';
fid = fopen('csvlist4.csv','r+');
fprintf(fid,c);
How it works: the data in csv is an array. I put data in 3rd row, so first and second is an empty but they have ','. When you use fprintf it will replace this , with your text. So if text is too long it will overwrite your data.
How to avoid this?
Easiest way - is to do it with help of xlswrite function. For your example:
txt = cell(size(Q))
txt{1} = 'this is your text'
X = [txt; num2cell(Q)]
xlswrite('new.xlsx',X)
Result:
Important moment here: number of cell for text must be the same as your data. But I filled with the text only first cell in my example.
And the one more way: read csv file, modify it's data and write to csv:
csvwrite('csvlist4.csv',a,2,0);
m = csvread('csvlist4.csv');
fid = fopen('csvlist4.csv','w');
c = 'your text'
fprintf(fid, c); fprintf(fid, '\n');
fclose(fid);
dlmwrite('csvlist4.csv', m(3:end,:), '-append');
Of course you can use cell array instead c and so on and so on.
Hope it helps!

How to put conversion operation in a for loop?

Below is the code to convert .tim file to ascii file for one particular file. But what I need is to convert 500 files(.tim). I also need to save the .ascii file in SAME name as the .tim file name like below for all 500 files.
bin=fopen('file_01.tim','r');
ascii = fread(bin, [43,21000], 'float32');
data_values=ascii';
dlmwrite('file_01.xls', data_values, 'delimiter', '\t', ...
'precision', '%.6f','newline','pc');
Using a "for loop" to do the conversion and save the ascii file with the same name of the tim, was my first thought but I don't know how to that.
You can use dir to get a list of all the filenames in your folder and then proceed just as you have but using replacing 'file_01.tim' with [D(ii).name]
e.g.
D = dir('*.tim');
for ii = 1:size(D,1)
bin=fopen(D(ii).name,'r');
%your processing etc
savename = [strtok(D(ii).name,'.'), '.xls']; %Change the file ext from .tim to .xls
dlmwrite(savename, ...
 

How to read line from a text file as a string in matlab?

I am trying to read a text file in MATLAB which has a format like the following. I am looking to read the whole line as a string.
2402:0.099061 2404:0.136546 2406:0.447161 2407:0.126333 2408:0.213803 2411:0.068189
I tried couple of things.
textscan(fid, '%s') reads the line but splits the line into cells at spaces.
fscanf(fid, '%s') reads the line as a string but removes all the spaces.
fgetl(fid) will do what you're looking for. Newline is stripped off.
textscan uses a whitespace delimeter by default. Set the delimiter to an empty string:
>> q = textscan(fid, '%s', 'Delimiter', '');
>> q{1}{:}
ans = 2402:0.099061 2404:0.136546 2406:0.447161 2407:0.126333 2408:0.213803 2411:0.068189
If you want to read the whole file as string (your file has only one line), try:
s = fileread('input.txt'); %# returns a char vector
s = strtrim(s); %# trim whitespaces
If you look at the source code of FILEREAD function, it is basically reading the file in binary mode as an array of characters: fread(fid, '*char')
whitespace is treated as a delimiter by default with textscan.
specify a different delimiter (that is not present in your data) when calling, that should do the trick, add this f.e.
'delimiter', '|'
you can also use
file = textread(<fileref goes here>, '%s', 'delimiter', '\n')
then
file{1,1}
will return
ans =
2402:0.099061 2404:0.136546 2406:0.447161 2407:0.126333 2408:0.213803 2411:0.068189
hope this helps
Use:
clc;
fid = fopen('fileName.m');
while ischar(tline)
disp(strcat("Line imported: ",tline))
tline = fgetl(fid);
end
fclose(fid);