views:

51

answers:

1

Basically I export .dat files, with headers, from a software and then I use data in them in Matlab. I am making a general function in Matlab which could read the data from the .dat files I export from the software. Can you tell me the way I can input the name of the .dat file as function's argument?

function [property_without_headers]=gslib_file_to_matlab_var(nModel,nCell)

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

gslib_file_name=input('Enter the gslib file name: ','s');
property_gslib_format=textread('gslib_file_name.dat','%f\t','headerlines',nModel+2);
property_without_headers=reshape(property_gslib_format,nModel,nCell)';

So I was thinking that file's name stored as string gslib_file_name could be used in gslib_file_name.dat as the textread argument. But its not working that way.

+2  A: 
function [property_without_headers]=gslib_file_to_matlab_var(nModel,nCell,gslib_file_name) 

if nargin<3, % making third argument optional (code to be executed 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
property_gslib_format=textread(gslib_file_name,'%f\t','headerlines',nModel+2); 
% ...
rwong
Hi rwong: If my file name is `perm_mudstone_channel_sgsim_gslib_format` and I include it it function's argument, then its giving me error as _??? Undefined function or variable 'perm_mudstone_channel_sgsim_gslib_format'._ However its working good without taking the file's name as argument.
Harpreet
In MATLAB, strings are treated as "row matrix of characters" and are surrounded by single quote. So, try `'perm_mudstone_channel_sgsim_gslib_format'`
rwong
Works! Thanks dude :)
Harpreet