views:

71

answers:

3

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')
+3  A: 

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
eakbas
I tried your lines, however if I write the file name without quotes, then it doesn't proceeds further to those lines. It gives this error: _??? Undefined function or variable 'poro_models_5wells_gslib_format'._
Harpreet
If you write it without quotes, MATLAB thinks that it is either a variable or a function and when neither is true, you get an "undefined function or variable" error before your function (gslib_file_to_matlab_var) is called. So the lines I recommended you are not even reached at all.
eakbas
+2  A: 

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.

gnovice
+2  A: 

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.

Jonas