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, ...
Related
I want to read the data of a file with size about 60 MB into matlab in some variables, but I get errors. This is my code:
clear all ;
clc ;
% Reading Input File
Dataz = importdata('leak0.lis');
%Dataz = load('leak0.lis');
for k = 1:1370
foundPosition = 1 ;
for i=1:size(Dataz,1)
strp = sprintf('I%dz=',k);
fprintf(strp);
findValue = strfind(Dataz{i}, strp) ;
if ~isempty(findValue)
eval_param = strp + '(foundPosition) = sscanf(Dataz{i},''%*c%*c%*f%*c%*c%f'') ;';
disp(eval_param);
% str(foundPosition) = sscanf(Dataz{i},'%*c%*c%*f%*c%*c%f') ;
eval(eval_param);
foundPosition = foundPosition + 1 ;
end
end
end
When I debugged it, I found out that the dataz is empty & so it doesn't proceed to next lines. I replace it with fopen, load & etc, but it didn't work.
From the Matlab help files, import data is likely failing because it doesn't understand your file format.
From the help files
Name and extension of the file to import, specified as a string. If importdata recognizes the file extension, it calls the MATLAB helper function designed to import the associated file format (such as load for MAT-files or xlsread for spreadsheets). Otherwise, importdata interprets the file as a delimited ASCII file.
For ASCII files and spreadsheets, importdata expects to find numeric
data in a rectangular form (that is, like a matrix). Text headers can
appear above or to the left of the numeric data, as follows:
Assuming that your .lis files actually have delimited text.
You should adjust the delimiter in the importdata call so that Matlab can understand your file.
filename = 'myfile01.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
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')
I need to extract the first value of the following code:
3.43099,70.8539,91.701,FAIL
The file has the '.sol' extension, but it can be read in notepad or Matlab.
I just want to know how to to read in all *.sol files in a folder and how to write the extracted value in a text file.
Thanks a lot, i would be grateful.
NEW
ita='"';
for i=1:size(z,2)
word_to_replace=input('Replace? ','s');
tik=input('Replacement? ','s');
coluna=input('Column? ');
files = dir('*.txt');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w');
val=num2str(z(i,coluna));
word_replacement=strcat(tik,val,ita);
contents = regexprep(contents,'word_to_replace','word_replacement');
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
end
Many thanks
File extension does not make a difference as to what MATLAB can "read", use the fileread command to load in the file and parse its contents. You can then split on commas, since it looks like it is comma separated
files = dir('*.sol');
fh = fopen('outFile.txt','w');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
parts = regexp(contents,',','Split');
fprintf(fh,[parts{1},'\n']);
end
end
fclose(fh)
This should do what you want. It will find all files in the current directory with the .sol extension, loop through all of them, grab the first value, and write it out to a text file.
Find and replace
Finding and replacing is relatively simple as well. You can do the same looping, read the file contents, run a replacement, and then rewrite that out to the same file.
files = dir('*.sol');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w'); % open handle to same file just read for overwriting
contents = regexprep(contents,'toReplace','replacement'); % do string replacement
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
I have some csv files that are named as:
eddypro_CFCT_201206_full_output_2013-06-26T121839.csv
eddypro_CFCT_201207_full_output_2013-06-26T160648.csv
.....
The files are mainly sorted by year and month. The characters after output are only some random numbers and letters, but the length will be same all the time.
Is there a way that i can import all these csv files together in matlab?
Part of my old code like this that can only read the files named as: eddypro_CFCT_01_full_output.csv, eddypro_CFCT_02_full_output.csv, and so on.
EddyproPath = 'C:\Users\CFCT_test\';
numfiles = length(dir([EddyproPath '\*.csv']));
for n = 1:numfiles
FilePath = [EddyproPath,'eddypro_CFCT_',num2str(n,'%02d'),'_full_output.csv'];
fid = fopen (FilePath, 'rt');
You are already using dir to get the number of files, so why not use dir to get the file list. Or am I misunderstanding?
d = dir([EddyproPath '\*.csv']);
for ii=1:numel(d),
fid = fopen(fullfile(EddyproPath,d(ii).name),'rt');
% ...
end
If for whatever reason dir is not dictionary sorting the file names, you can do sort({d.name}).
To get similar files among different text files I've used ismember()
file1 = {'DSC01605.bmp';'Hampi8.bmp';'DSC01633.bmp';...
'DSC01198.bmp';'DSC01619.bmp'}
file2 = {'DSC01605.bmp';'Hampi8.bmp';'DSC01633.bmp'}
file3 = {'DSC01605.bmp';'Hampi8.bmp'}
matching12 = ismember(file1, file2)
matching13 = ismember(file1, file3)
matchesAll3 = matching12 & matching13
allMatchingStrings = file1(matchesAll3)
Now allMatchingStrings contains
'DSC01605.bmp'
'Hampi8.bmp'
How can i write these files to a new text file all.txt? Problem with my requirements is - suppose allMatchingStrings contains around 10 files, but i need only 5 out of those 10 files. I need to save 5 files to a new text file say all.txt. How can i do that?
A quick way to write them to disk is with the fprintf command.
fid = fopen('all.txt', 'w');
fprintf(fid, '%s\n', allMatchingStrings{:});
fclose(fid);
If you only wanted to write the first 2 filenames in allMatchingStrings then you could limit like this:
filenamesIWant = 1:2;
fid = fopen('all2.txt', 'w');
fprintf(fid, '%s\n', allMatchingStrings{filenamesIWant});
fclose(fid);
This works because the fprintf command repeats for each string you give it. The only trick is getting the curly brackets int he right place.