Batch modifying xls files and save it as txt file in matlab - excel

I have a code which reads a an excel file and modifies the xls fil and then save it as an text file
[f,n] = uigetfile('*.xls');
[num,text,row]=xlsread(f);
data=num';
temp_dir = pwd;
[f,n]=uiputfile('*.txt');
if isstr(f)
cd(n);
fid=fopen(f,'w');
[rows,cols]=size(text);
for i=1:rows
fprintf(fid,'%s\t',text{i,1:end-1});
fprintf(fid,'%s\n',text{i,end});
end
plats = '%10f\t';
[rows,cols] = size(data);
for n = 1:rows-2
plats = [plats,'%10f\t'];
end
now I have like 100 xls file and I want to this process for all of them like batch processing.
I know that I could use :
files_from= dir(fullfile(from_dir,'*.xls'));
for i = 1:length(files_from)
FileName=files_from(i).name;
[num,txt,all]= xlsread(fullfile(from_dir,FileName));
xlswrite(fullfile(to_dir, files_from(i).name),data);
end
but I can't get it write :((((((((
please any suggestion?????

Related

how to load files into AudioSegment.from_file

I am trying to make a python script which can convert a file from 'm4a' to a '.wav' however it is not able to find the files, returning "[WinError 2] The system cannot find the file specified" every time.
Paths[0] include the full path of the file in question
m4a_file = paths[0]
wav_filename = filename + ".wav"
#track = AudioSegment.from_file(m4a_file, format= 'm4a')
track = AudioSegment.from_file(file = relative_path,
format = 'm4a')
file_handle = track.export(wav_filename, format='wav')
// I've tried to
I've given both relative and full path for the file in question
'c:\Users\user\....\Workspace\Pollux\my_file.m4a'
and
'my_file.m4a'

Python read oldest file first

I have object and attributes data in separate csv files. there are 3 different types of objects.
Directory may contain different files but I have to read and process object and attribute files. After reading the object file and then will have to read respective attribute file.
Below is code and files
plant = []
flower = []
person = []
for file_name in os.listdir(dir_path):
if os.path.isfile(os.path.join(dir_path, file_name)):
if file_name.startswith('plant_file'):
plant.append(file_name)
if file_name.startswith('person_file'):
person.append(file_name)
if file_name.startswith('flower_file'):
flower.append(file_name)
for file_name in person:
object_file_path = dir_path + file_name
attribute_file_path = dir_path + file_name.replace('file','attributes_file')
read_object_csv = pd.read_csv(object_file_path)
read_attribute_csv = pd.read_csv(attribute_file_path)
for file_name in flower:
object_file_path = dir_path + file_name
attribute_file_path = dir_path + file_name.replace('file','attributes_file')
read_object_csv = pd.read_csv(object_file_path)
read_attribute_csv = pd.read_csv(attribute_file_path)
file name contains date and time in the format YYYYMMDDHHMMSS . Sample file names are
plant_attributes_file_20221013134403.csv
plant_attributes_file_20221013142151.csv
plant_attributes_file_20221013142455.csv
plant_file_20221013134403.csv
plant_file_20221013142151.csv
plant_file_20221013142455.csv
person_file_20221012134948.csv
person_file_20221012140706.csv
person_attributes_file_20221012134948.csv
person_attributes_file_20221012140706.csv
How can we sort file names in list using timestamp, so that oldest file can be loaded first and load latest file at last ?

VBA read large Linux-formatted text file starting from specific line without reading whole file into memory

I have a large Linux-formatted (i.e. linefeed character for a new line) text file that is a "status" file for a program as it runs. This file is updated while the program is running. It outputs some useful messages and periodic header information, but also lines of numerical status data separated by spaces. Because it is a piece of commercial software (Abaqus /Explicit), I can't do anything to modify how it writes to this status file.
I wrote a VBA script awhile back that opens the file, reads it in as a whole via "Input$", splits the file into lines via "Split(Data, vbLF)", and parses each line to extract the numerical status data and place it into an Excel spreadsheet for me to view and analyze. The problem is that, as the program continues to run, this file gets larger and larger to the point that reading the whole file into Excel is becoming impractical.
I've been trying to figure out a quick way to read the file starting from the line I left off on, without reading the entire thing to memory. I tried to save EOF() to a "helper" cell on the spreadsheet and use that with "Seek" and "Line Input" to jump right to the spot I left off on, but that won't work because "Line Input" doesn't recognize the linefeed character as a new line, only carriage return or carriage return-linefeed.
I spent some time going through other posts and it seemed like the only other solutions required reading the whole file in. Does anyone have any ideas as to how this could be done WITHOUT reading the whole file in?
Thanks for your time!
'perhaps this could be modified for new start
Sub read_bookmark()
Dim linein, tline, endmark, linevalid, filelen, fread
Dim chx As Byte
Dim fpath, mes, a
On Error GoTo ErrHand
mes = "Enter path & filename of Bookmark import file."
'fpath = InputBox(mes, "", "c:\temp\bookmarks_12_8_18.html")
fpath = "c:\temp\test.txt"
If Len(fpath) = 0 Or (Len(Dir(fpath)) = 0) Then
MsgBox ("File not found. Program canceled")
End
End If
tline = ""
linein = ""
endmark = False
linevalid = False
Open fpath For Binary Access Read As #1
filelen = LOF(1) ' Get length of file.
For fread = 1 To filelen
Get #1, , chx
If chx = 13 Or chx = 10 Then
endmark = True
ElseIf endmark = True Then 'wait till no more endmarks
endmark = False
tline = linein
linevalid = True
linein = Chr(chx) 'keep chr after endmarks
Else
linein = linein + Chr(chx)
End If
If linevalid = True Then
'proc tline
tline = ""
linevalid = False
End If
Next
'proc last linein chrs
MsgBox ("Processing Complete")
Exit Sub
ErrHand: ' Error-handling routine.
Close #1
MsgBox "Error in HTML read " + Error(Err)
End
End Sub

Using ActiveX to Import from Excel to Matlab

I'm in need of optimizing import of .xls files to matlab due to xlsread being very time consuming with large amount of files. Current xlsread script as follows:
scriptName = mfilename('fullpath');
[currentpath, filename, fileextension]= fileparts(scriptName);
xlsnames = dir(fullfile(currentpath,'*.xls'));
xlscount = length(xlsnames);
xlsimportdata = zeros(7,6,xlscount);
for k = 1:xlscount
xlsimport = xlsread(xlsnames(k).name,'D31:I37');
xlsimportdata(:,1:size(xlsimport,2),k) = xlsimport;
end
I have close to 10k files per week that needs processing and with approx. 2sec per file processed on my current workstation, it comes in at about 5½ hours.
I have read that ActiveX can be used for this purpose however that is far beyond my current programming skills and have not been able to find a solution elsewhere. Any help on how to make this would be appreciated.
If it is simple to perform with ActiveX (or other proposed method), I would also be interested in data on cells D5 and G3, which I am currently grabbing from 'xlsnames(k,1).name' and 'xlsnames(k,1).date'
EDIT: updated to reflect the solution
% Get path to .m script
scriptName = mfilename('fullpath');
[currentpath, filename, fileextension]= fileparts(scriptName);
% Generate list of .xls file data
xlsnames = dir(fullfile(currentpath,'*.xls'));
xlscount = length(xlsnames);
SampleInfo = cell(xlscount,2);
xlsimportdata = cell(7,6,xlscount);
% Define xls data ranges to import
SampleID = 'G3';
SampleRuntime = 'D5';
data_range = 'D31:I37';
% Initiate progression bar
h = waitbar(0,'Initiating import...');
% Start actxserver
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
for k = 1:xlscount
% Restart actxserver every 100 loops due limited system memory
if mod (k,100) == 0
exl.Quit
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
end
exlFile = exlWkbk.Open([dname filesep xlsnames(k).name]);
exlSheet1 = exlFile.Sheets.Item('Page 0');
rngObj1 = exlSheet1.Range(SampleID);
xlsimport_ID = rngObj1.Value;
rngObj2 = exlSheet1.Range(SampleRuntime);
xlsimport_Runtime = rngObj2.Value;
rngObj3 = exlSheet1.Range(data_range);
xlsimport_data = rngObj3.Value;
SampleInfo(k,1) = {xlsimport_ID};
SampleInfo(k,2) = {xlsimport_Runtime};
xlsimportdata(:,:,k) = xlsimport_data;
% Progression bar updater
progress = round((k / xlscount) * 100);
importtext = sprintf('Importing %d of %d', k, xlscount);
waitbar(progress/100,h,sprintf(importtext));
disp(['Import progress: ' num2str(k) '/' num2str(xlscount)]);
end
%close actxserver
exl.Quit
% Close progression bar
close(h)
Give this a try. I am not an ActiveX Excel guru by any means. However, this works for me for my small amount of test XLS files (3). I never close the exlWkbk so I don't know if memory usage is building or if it automatically cleaned up when descoped after the next is opened in its place ... so use at your own risk. I am seeing an almost 2.5x speed increase which seems promising.
>> timeit(#getSomeXLS)
ans =
1.8641
>> timeit(#getSomeXLS_old)
ans =
4.6192
Please leave some feedback if this work on large number of Excel sheets because I am curious how it goes.
function xlsimportdata = getSomeXLS()
scriptName = mfilename('fullpath');
[currentpath, filename, fileextension]= fileparts(scriptName);
xlsnames = dir(fullfile(currentpath,'*.xls'));
xlscount = length(xlsnames);
xlsimportdata = zeros(7,6,xlscount);
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
dat_range = 'D31:I37';
for k = 1:xlscount
exlFile = exlWkbk.Open([currentpath filesep xlsnames(k).name]);
exlSheet1 = exlFile.Sheets.Item('Sheet1'); %Whatever your sheet is called.
rngObj = exlSheet1.Range(dat_range);
xlsimport = cell2mat(rngObj.Value);
xlsimportdata(:,:,k) = xlsimport;
end
exl.Quit

Writing data to excel files, MATLAB

I am using MatlabR2011a on my Windows 7 machine.
I have a folder of 635 text files. Each file contains multiple rows and 2 columns. I am trying to loop through the folder and read each file and write these values to excel. So far this is what I have:
close all; clc;
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.txt'))
for k = 1:635
j =1
filename = fullfile(dirname,Files(k).name);
fid = fopen(filename);
x = fgetl(fid)
while ischar(x)
x = fgetl(fid)
a2 = strtrim(x);
a3 = textscan(a2,'%s');
a4 = a3{1}{1};
a5= a3{1}{2};
pos3 = strcat('A',num2str(j));
xlswrite('sample_output',{a4,a5},'Sheet1',pos3)
j = j+1;
end
fclose(fid);
end
It keeps giving me the following error after finishing reading the first file.
Error using ==> strtrim Input should be a string or a cell array of strings.
A sample input file looks like this:
15076 4636259
15707 4636299
15714 1781552
15721 4204950
15730 2174919
16209 4636510
16413 4758572
16470 4445808
17519 311397
17667 2116489
17739 1729694
18024 3210756
18627 3714194
18695 4192858
19141 632766
19318 1923574
19438 1255216
19493 4635020
19771 4770250
How can I fix this? Would appreciate help with this!
In the while loop, cut the line
x=fgetl(fid)
...and paste it before the end statement right after j=j+1.
The error occurs because you get a line before the while statement, then you use the while statement to test validity of the string (all fine so far), but then immediately get the subsequent line before you perform your string operation. The call to fgetl at the start of the while block could return an EOF, causing subsequent string manipulation functions to fail.
Also... The code would be more robust if you set your for loop like so
for k=1:numel(Files)
Since all your data are numeric this should work. Give it a try.
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.txt'))
j =0;
for k = 1:numel(Files)
filename = fullfile(dirname,Files(k).name);
x = dlmread(filename,'\t'); %# I assume tab-delimiter
j = j + size(x, 1);
xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
end

Resources