如何向用户明确函数的参数在 Matlab 中以字符串形式输入?

发布于 2024-09-25 13:09:00 字数 1088 浏览 1 评论 0原文

我有以下函数,其最后一个参数是文本文件的名称:

function [property_without_headers]=gslib_file_to_matlab_var(nModel,nCell,gslib_file_name) % must enter the last argument, gslib_file_name, in single quotes as its treated as a string

if nargin<3, % making third argument optional (code to be executed even when only 2 arguments are provided)
    gslib_file_name=input('Enter the gslib file name: ','s');
end
if length(gslib_file_name)<4 || ~strcmpi(gslib_file_name(end-3:end),'.dat'),
    gslib_file_name=[gslib_file_name '.dat']; % string concatenation
end

%% Reading directly from the .dat files generated from SGEMS and making the data of file as a variable

property_gslib_format=textread(gslib_file_name,'%f\t','headerlines',nModel+2); 
property_without_headers=reshape(property_gslib_format,nModel,nCell)';

现在,根据一般看法,该函数要输入的最后一个参数是数字。 如何让用户更清楚地知道要输入的最后一个参数(即文本文件的名称)应该采用字符串格式,即用单引号引起来? 如果我定义了函数如下所示,然后我收到意外的 MATLAB 表达式的错误。

[property_without_headers]=gslib_file_to_matlab_var(nModel,nCell,'gslib_file_name')

I have the following function whose last argument is a name of the text file:

function [property_without_headers]=gslib_file_to_matlab_var(nModel,nCell,gslib_file_name) % must enter the last argument, gslib_file_name, in single quotes as its treated as a string

if nargin<3, % making third argument optional (code to be executed even when only 2 arguments are provided)
    gslib_file_name=input('Enter the gslib file name: ','s');
end
if length(gslib_file_name)<4 || ~strcmpi(gslib_file_name(end-3:end),'.dat'),
    gslib_file_name=[gslib_file_name '.dat']; % string concatenation
end

%% Reading directly from the .dat files generated from SGEMS and making the data of file as a variable

property_gslib_format=textread(gslib_file_name,'%f\t','headerlines',nModel+2); 
property_without_headers=reshape(property_gslib_format,nModel,nCell)';

Right now it seems, through general perception, that the function's last argument to be entered is numeric. How can I make it more clear for the user that the last argument, the name of the text file, to be entered should be in string format i.e. in single quotes? If I define the last argument of the function like following then I get an error of Unexpected MATLAB expression.:

[property_without_headers]=gslib_file_to_matlab_var(nModel,nCell,'gslib_file_name')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给不了的爱 2024-10-02 13:09:00

要做的第一件事可能是为该函数编写“帮助”,并明确指出最后一个参数必须是字符串。

您可以使用“ischar()”检查该参数的类型,即:

if ~ischar(gslib_file_name)
    error('gslib_file_name  should a be string');
end

The first thing to do might be writing a "help" for the function, and explicitly mention that the last argument must be a string.

And you could check the type of that argument by using "ischar()", that is:

if ~ischar(gslib_file_name)
    error('gslib_file_name  should a be string');
end
泅人 2024-10-02 13:09:00

这就是帮助文档的用途,这就是您的功能的最终用户确实会看到。可以在此处找到更多格式建议。

This is what help documentation is for, which is what the end user of your function is really going to see. Some more formatting suggestions can be found here.

小傻瓜 2024-10-02 13:09:00

如果您的应用程序不需要仅在控制台上运行,我建议使用 uigetfile< /a> 让用户通过 GUI 对话框窗口选择文件。因此,用户会非常清楚您正在寻找文件名。

因此,你会写

if nargin < 3
     %# ask for a *.dat file
     [fileName,pathName] = uigetfile('*.dat','select gslib file');
     %# check whether the user selected anything
     if fileName == 0
        error('file selection aborted by user')
     end
     %# construct gslib file name from path and file name
     gslib_file_name = fullfile(pathName,fileName);
end

显然,无论如何,很好地记录该函数都会有所帮助。

If your application isn't required to run on a console only, I suggest using uigetfile to have the user select a file via a GUI dialog window. Thus, it will be very clear for the user that you're looking for a file name.

Thus, you'd write

if nargin < 3
     %# ask for a *.dat file
     [fileName,pathName] = uigetfile('*.dat','select gslib file');
     %# check whether the user selected anything
     if fileName == 0
        error('file selection aborted by user')
     end
     %# construct gslib file name from path and file name
     gslib_file_name = fullfile(pathName,fileName);
end

Obviously, documenting the function well will help anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文