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

조회 수: 3 (최근 30일)
Hans Jørgen Jensen
Hans Jørgen Jensen 2017년 8월 18일
댓글: Hans Jørgen Jensen 2017년 8월 22일
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
Stephen23
Stephen23 2017년 8월 18일
@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 2017년 8월 20일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KL
KL 2017년 8월 21일
편집: KL 2017년 8월 21일
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
Hans Jørgen Jensen
Hans Jørgen Jensen 2017년 8월 22일
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');

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by