MATLAB Answers

Qingyang
1

Check if filename is valid.

Qingyang
さんによって質問されました 2012 年 7 月 20 日
最新アクティビティ Thomas
さんによって 回答されました 2017 年 12 月 1 日
Hi, is there a native function or a simple code to check if a user-typed filename is valid for saving a file? (In that it doesn't contain illegal characters like /\":?<>|, etc.)

  0 件のコメント

サインイン to comment.

4 件の回答

Sean de Wolski
回答者: Sean de Wolski
2012 年 7 月 20 日
 採用された回答

regexp it!

  3 件のコメント

Qingyang
2012 年 7 月 20 日
So it would be like this?
if regexp(fname, '[/\*:?"<>|]', 'once')
do something about it?
end
Jan
2012 年 7 月 23 日
Let UIPUTFILE check the file name instead of using an edit control.
Jason Nicholson 2014 年 2 月 14 日
This code snippet will not work unless you check if it is empty.
if ~isempty(regexp(filename, '[/\*:?"<>|]', 'once'))
%Do something
end

サインイン to comment.


Daniel Shub
回答者: Daniel Shub
2012 年 7 月 23 日

A long time ago, probably Windows 95 on a FAT16 harddisk, I started using the following:
regexp(fname, ['^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)', ...
'(\..+)?$)[^\x00-\x1f\\?*:\"><|/]+$'], 'once'));
At that time, Windows had the most restrictive file name conventions. I am not sure what the rules are now. You also should check for the length of the path and the total number of characters in the file name.

  1 件のコメント

Jan
2012 年 7 月 23 日
Windows 7: Either 260 characters, or start with //?/ and 32767 characters, while each folder can have up to 255 characters. The later works for most of the API-functions, but not e.g. for the Windows Explorer. Moving a file name with a long name to the RecycleBin fails also. The backup program and server based tools might fail also. Therefore I'd prefer the dull limit of 260 characters (including "C\:" and a trailing terminator '\0')!).

サインイン to comment.


ola bayo
回答者: ola bayo
2017 年 10 月 16 日
編集済み: Walter Roberson
2017 年 10 月 16 日

Which following is a valid file name in MATLAB?
A) lab 2
B) lab2
C) lab-2
D) 2lab
E) LAB 2

  1 件のコメント

Walter Roberson
2017 年 10 月 16 日
For the question as phrased: all of them. For a slightly different question, the answer would be fairly different.

サインイン to comment.


Thomas
回答者: Thomas
2017 年 12 月 1 日

If you do not want to use UIputfile, another option is fopen. fileID = fopen(filename,'w') if fopen cannot open the file, then fileID is -1.

  0 件のコメント

サインイン to comment.



Translated by