Converting cell components into strings for moving files

조회 수: 4 (최근 30일)
Sanwal Yousaf
Sanwal Yousaf 2015년 10월 9일
댓글: Sanwal Yousaf 2015년 10월 11일
I am working on a code that helps me move files that i choose from one directory to another . The code that i have is as follows.
[filename, pathname,filterindex] = uigetfile('*.m', 'Select the Matlab files to move', 'MultiSelect', 'on')
source = strcat(pathname, filename)
destination = uigetdir
opt_choose = inputdlg('What do you want to do:/n 1. Move all files or 2. Move one file',...
'Specify your interest in the function', [1,30])
option = str2num(opt_choose{:});
if option < 1||option >2
%This is to make sure that the input is only 1 or 2.
errordlg('This is not an option. You have only 2 more attempts')
end
if option ==1
movefile(source,destination)
end
The error that i get is as follows :
Error using movefile Argument must contain a string.
Error in move_it (line 13) movefile(source,destination)
I believe it is because the files that i am selecting, which are multiple files, are being stored as cell components. When i check for the value of source in the workspace, it is as follows,
source = 'abcd. m' 'xyzd.m'
How could i change this to a string so that i could pass it into the if function as a source so that it wont give me an error.
Any help would be appreciated.

채택된 답변

Nobel Mondal
Nobel Mondal 2015년 10월 9일
편집: Nobel Mondal 2015년 10월 9일
Replace the line calling 'movefile' by a cell-function. Like this -
cellfun(@(C) movefile(C, destination), source)
  댓글 수: 2
Sanwal Yousaf
Sanwal Yousaf 2015년 10월 11일
just to clarify. What is C representing in this function? is that the source or something else?
Sanwal Yousaf
Sanwal Yousaf 2015년 10월 11일
Never mind. Apologies. This works like a charm. Thanks a lot.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 10월 9일
movefile() accepts only the name of one file (or directory) at a time. If you have multiple files then you will need to movefile() them one by one.
Note: you should use fullfile() instead of strcat() to construct the path.
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 10월 11일
Use sprintf() to construct a string that is an operating system command and system() the command.
Sanwal Yousaf
Sanwal Yousaf 2015년 10월 11일
I got it. i was making a syntax error in my script as well. However, fullfile worked like a charm. Thanks a lot!

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

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by