how to update filename in a call to an external application ?

3 visualizaciones (últimos 30 días)
I just learned how to run a file with an external application. And that works. The application converts a datafile to CSV. The command looks like this:
!C:\Users\hjj019\Desktop\DLConverter.exe C:\Users\hjj019\Desktop\CA\TMWMU645.228
My next challenge, is to change the datafile file name so the routine converts one file after another. I worked out the code below, to find the path of the next file in my folder, but the command does not accept it as a valid file name. First I tried to use datastore, but since my datafiles have all sorts of random file extensions, I gave up on that, and tried a different way.
Here is what I have:
% Specify data location
dataloc = 'C:\Users\hjj019\Desktop\';
location1 = fullfile(dataloc,'CA');
[M,N] = size(dir(location1)) % Number of datalog files
for i = 1:M
files=dir(location1);
files1=files(M,1)
files2=files1.name
files3 = fullfile(location1,files2)
% Convert datalog files to CSV
_italic_
!C:\Users\hjj019\Desktop\DLConverter.exe files3
end
COMMAND WINDOW:
files1 = struct with fields:
name: 'TMWMU645.228'
folder: 'C:\Users\hjj019\Desktop\CA'
date: '13-jan-2015 09:36:14'
bytes: 25584
isdir: 0
datenum: 7.3598e+05
files2 = 'TMWMU645.228'
files3 = 'C:\Users\hjj019\Desktop\CA\TMWMU645.228'
Error: Could not open file files3
So my question is: How can I insert an updating filename in my file conversion command ?
  3 comentarios
Stephen23
Stephen23 el 18 de Ag. de 2017
@Hans Jørgen Jensen: it makes no sense to call dir inside the for loop: you only need to call dir once and loop over the structure that it returns. You will find very clear examples in the MATLAB help:
or on this forum:
and I would highly recommend that you follow those examples.
Hans Jørgen Jensen
Hans Jørgen Jensen el 20 de Ag. de 2017
Hi Stephen, thanks for your comments and links. I am not a programmer, so I need all the help I can get. Regarding use of the "!" command to open a file with a windows program: Do you know why it cannot open the file ? (Error: Could not open file files3)
If I type in this way:
!DLConverter.exe TMWMU645.228, it works fine, but if I find the name of the datafile (files3) via the control loop above and use the command like this:
!DLConverter.exe files3
I get this: Error: Could not open file files3
I notice that files3 is in single quotation marks, like this: 'datafile001.456'. Could that be the problem ?
I appreciate any help I can get. I have looked a lot on the community, but not found an answer. Thanks.

Iniciar sesión para comentar.

Respuesta aceptada

KL
KL el 21 de Ag. de 2017
Editada: KL el 21 de Ag. de 2017
Hi Hans Jørgen Jensen ,
I've tried your code and yeah, I see your problem. I don't understand it but probably you could simply use system command to accomplish your task. Something like this,
% Specify data location
dataloc = 'C:\Users\hjj019\Desktop';
location1 = fullfile(dataloc,'CA');
[M,N] = size(dir(location1)) % Number of datalog files
files=dir(location1);
for iFile = 3:M
fileDetails=files(iFile,1)
fileName = fullfile(location1,fileDetails.name)
% Convert datalog files to CSV
system(['C:\Users\hjj019\Desktop\DLConverter.exe ' fileName])
end
  1 comentario
Hans Jørgen Jensen
Hans Jørgen Jensen el 22 de Ag. de 2017
Thanks KL, I just got the same answer from someone else this morning, and it works :-)
FileName = files3;
% Convert datalog files to CSV
ConverterCmd = ['"C:\Users\hjj019\Desktop\CA\DLConverter.exe" "' FileName '"'];
[status,cmdout] = system(ConverterCmd,'-echo');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by