How do I check if a folder exists which has a variable name?

Hi,
I want to check if the created folder already exists. But the name of the folder depends on the file I work with.
fname = Measurement_20190829_18_15_10.bin
if ~exist('Figures' fname(11:end-4), 'dir')
mkdir('Figures', fname(11:end-4));
end
I have no clue how to make this work.
I always get Invalid expression with a marker on fname.
Please help.
Thank you!

3 件のコメント

Stephen23
Stephen23 2019 年 8 月 29 日
編集済み: Stephen23 2019 年 8 月 29 日
Splitting the extension from a filename using indexing is very fragile and a bad way to write code. The simple recommended method is to use fileparts:
[~,name,ext] = fileparts(...)
Does this really run without error?:
fname = Measurement_20190829_18_15_10.bin
What do you expect this syntax to do?:
exist('Figures' fname(11:end-4), 'dir')
% ^^^^^^^^^^^^^^^^^^^^^^^^^ what is this supposed to do?
When I try it, it throws an error.
David Müller
David Müller 2019 年 8 月 29 日
Hi, thanks for your fast response.
No, i am reading the file not like this.
How does fileparts help me in this example?
My code looks like this:
[fname,pathname] = uigetfile( '.bin' , 'Messfile auswählen');
if ( fname == 0 )
return;
end
Data_File = fopen([pathname fname],'r');
%Some Code for calculation etc.
if ~exist('Figures'fname(11:end-4), 'dir')
mkdir('Figures', fname(11:end-4));
end
My file ist called: Messfile_20190829_18_15_10
and the folder created is called 20190820_18_15_10
But the exist does not work properly.
Stephen23
Stephen23 2019 年 8 月 29 日
編集済み: Stephen23 2019 年 8 月 29 日
"How does fileparts help me in this example?"'
Instead of using this fragile, very non-robust code to obtain the filename without the extension:
fname(11:end-4)
you can simply use robust fileparts:
[~,name] = fileparts(fname);
"But the exist does not work properly."
exist does work properly. Read the last four lines of my previous comment.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2019 年 8 月 29 日
編集済み: Stephen23 2019 年 8 月 29 日

1 投票

[~,name] = fileparts(fname);
exist(fullfile('Figures',name), 'dir')~=7
Note that exist does NOT return a boolean value, it returns different integers for specific object types. It is strongly recommended to test for the specific integer that is returned for the specific object type that you are trying to locate.

1 件のコメント

David Müller
David Müller 2019 年 8 月 29 日
Thank you so much - it works perfectly!

サインインしてコメントする。

その他の回答 (1 件)

Johannes Fischer
Johannes Fischer 2019 年 8 月 29 日
編集済み: Johannes Fischer 2019 年 8 月 29 日

0 投票

Your code is trying to store in fname the content of the field 'bin' of the struct 'Measurement_20190829_18_15_10', which propably doesnt exist.
fname needs to be a char array:
fname = 'Measurement_20190829_18_15_10.bin';
I have no idea about your folder structure, but Im guessing its 'Figures\t_20190829_18_15_10' ? If so, you would need to write
mkdir(['Figures\', fname(11:end-4)]);
and the same as the argument for 'exist'

5 件のコメント

Stephen23
Stephen23 2019 年 8 月 29 日
It is recommended to use fullfile to construct paths, rather than concatenating strings.
David Müller
David Müller 2019 年 8 月 29 日
Hi, thanks for your response.
My code looks like this:
[fname,pathname] = uigetfile( '.bin' , 'Messfile auswählen');
if ( fname == 0 )
return;
end
Data_File = fopen([pathname fname],'r');
%Some Code for calculation etc.
if ~exist('Figures'fname(11:end-4), 'dir')
mkdir('Figures', fname(11:end-4));
end
My file ist called: Messfile_20190829_18_15_10
and the folder created is called 20190820_18_15_10
But the exist does not work properly.
Stephen23
Stephen23 2019 年 8 月 29 日
編集済み: Stephen23 2019 年 8 月 29 日
"But the exist does not work properly."
Exist works properly. Your error is because of this:
'Figures'fname(11:end-4)
Note how you simply place two character vectors next to each other, without concatenting them or as inputs to a function. You should use fullfile on them.
David Müller
David Müller 2019 年 8 月 29 日
Okay, but how should it look like with my code. Im new to MATLAB so sorry for all those questions. I dont get how to imlement fullfile...
Stephen23
Stephen23 2019 年 8 月 29 日
"..but how should it look like with my code."
See my answer.
It uses more robust fileparts rather than fragile indexing.
It uses simple fullfile to join the folder and file names.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 8 月 29 日

編集済み:

2019 年 8 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by