pepe
pepe

Reputation: 9909

Is there a way of getting a file name and inserting into Matlab script?

In a folder, I have both my .m file that contains the script and one imaging .dcm file that is the target of my analysis.

Folder structure:

Folder1/analysis.m
Folder1/meas_dynamic_123.dcm
Folder1/meas_123.dcm
Folder1/meas_345.dcm

My script (analysis.m) begins as follows:

target     =''; <== here should go only the filename that contains 'dynamic'
                    example: meas_dynamic_123.dcm

txt        = dir(target);

// etc

So I'm wondering if there is a way of when running analysis.m it will:

Does anyone have any pointers on how to achieve this? Using ffpath?

Upvotes: 0

Views: 99

Answers (3)

Sam Roberts
Sam Roberts

Reputation: 24127

Instead of which(scriptname), consider using mfilename('fullpath'), which will directly give you the full path of the running m-file. Using which might break if you had multiple commands of the same name shadowed on the path.

Upvotes: 0

Oli
Oli

Reputation: 16045

you should do:

f=dir('*dynamic*');
target=f(1).name;

Upvotes: 1

YYC
YYC

Reputation: 1802

Assume you are not in Folder1 and you need to find it:

script_name = 'analysis';
script_full_path = which(script_name);      % get full path of the script
script_dir = fileparts( script_full_path ); % get the directory of the script
file_list = dir( [script_dir '/*dynamic*']);
name_list = {file_list.name};

name_list will be a cell array containing the file names with the keyword 'dynamic'.

Upvotes: 1

Related Questions