0

Is there a way to use dir command in a function in MATLAB. I want to take argument in fiction and that argument to be part of dir command for e.g

function a = abc(num)
    aaa=dir('abc_num_*.csv);
end

I am getting error while doing it as num taken as an argument is not going in dir function. Is there a way to do it ?

2 Answers 2

0

In all programming and scripting laguages I have peeked into, something inside a pair of quotes as in 'quoted' and "also quuoted", has been literal text - to not be changed, ever.

So, you could vager that 'abc_'+str(num)+'_*.csv' or a similar construct would work for you.
Note that str(num) is common as method to convert a numerical value into a stream of characters. Such 'strings' can be appended/prepended to each other with e.g. + or similar.

0

There is a way to use an argument:

function a = abc(num)
    aaa = dir(['abc_' num2str(num) '_*.csv']);
    a = aaa.name;
end
  • Use [] to concatenate strings. The wildcard * is working.
  • Use num2str to convert a number into a string.
  • Don't forget to check if the aaa struct exists before getting its name field, for instance by using an if length(aaa >= 1) condition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .