Create a function which returns values into input variables in a single call.

For example, given a function:
function str=addslash(str) % add slash to a directory name
if str(end)~='\', str(end+1)='\'; end
end
Then I call it for a couple of directories:
dir1=addslash(dir1);
dir2=addslash(dir2);
dir3=addslash(dir3);
...
How to rearrange the function, in order to call it with one statement like this (number of input arguments may vary):
addslash(dir1, dir2, dir3, ...);
or like this:
addslash('dir1', 'dir2', 'dir3', ...); % dir1, dir2, dir3... are names of variables

댓글 수: 5

Why bother?
I don't see the point: fullfile handles the file separator without you needing to do any fiddling around with backslashes. I have written hundreds of programs that process millions of files, and have never wasted my time writing fiddly code to add backslashes onto folder names. Nor have I seen any code where this was done.
Why do this, when you can just use fullfile?
"addslash('dir1', 'dir2', 'dir3', ...); % dir1, dir2, dir3... are names of variables"
That would be a very slow, complex, buggy way to write code. Read this to know why:
The question is not merely about combining file names from file parts.
I did not say at all what it is needed for. There may be comparison of the directories, for instance.
"There may be comparison of the directories"
What exactly does that mean? What is the exact process involved in this "comparison"?
I am still confused how this would be useful: any MATLAB function that accepts absolute/relative filepaths can be used with fullfile, so I am genuinely interested in your use case.
i.e. strcmp(dir1,dir2);
If you just want to compare the paths, then removing the trailing file separator character would be simpler. See my answer.

답변 (2개)

function nargin=addslash(varargin) % add slash to a directory name
N = length(varargin) ;
nargin = cell(N,1) ;
for i = 1:N
str = varargin{i} ;
if str(end)~='\'
str(end+1)='\';
end
nargin{i} = str ;
end

댓글 수: 2

bbb_bbb
bbb_bbb 2018년 5월 3일
편집: bbb_bbb 2018년 5월 3일
The problem is, how to write the results back to the input variables?
@KSSV, that nargin should be varargout.
@bbb_bbb, The problem is, how to write the results back to the input variables?
If you need to do that, you've gone wrong in the design of your code. Clearly, since you want to apply the same code to multiple variables that means that these variables are related in some way. Therefore, you shouldn't have multiple variables but just one variable that holds the content of all these variables, an array, cell array, whatever container you want. the problem then goes away, just loop over the content of that container.
A very simple rule: If you number variables, stop, you're doing it wrong.
Stephen had a very valid point in the last part of his comment
Stephen23
Stephen23 2018년 5월 3일
편집: Stephen23 2018년 5월 3일
This will work on one single string or char vector, or on a cell array of them:
C = strcat(regexprep(C,'\\$',''),'\')
You can easily define a function handle for it:
addslash = @(C) strcat(regexprep(C,'\\$',''),'\');
Which makes calling it trivial:
>> addslash('hello')
ans = hello\
>> addslash('world\')
ans = world\
>> C = addslash({'ab','cd\','ef\gh'});
>> C{:}
ans = ab\
ans = cd\
ans = ef\gh\
EDIT: For your usecase, it would be simpler to remove the trailing slash:
strcmpi(regexprep(A,'\\$',''),regexprep(B,'\\$',''))

이 질문은 마감되었습니다.

태그

질문:

2018년 5월 3일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by