MATLAB: How to use load function?

fileloadtext file

i want to load a file but the problem is i don't want to use syntax load 'kl.txt' i want to use load filename where filename is assigned value of'kl.txt' I require this so that i can use load in functions so that i can pass filename aas argument .

Best Answer

Try this robust approach:
baseFileName = 'kl.txt';
folder = pwd; % Or whatever folder you want.
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
% File exists. Read it into a structure.
storedStructure = load(fullFileName);
% Extract individual variables from the structure.
a = storedStructure.a; % Or whatever the variable is called.

b = storedStructure.b; % Or whatever the variable is called.
else
% File does not exist. Alert user
warningMessage = sprintf('Error: mat file does not exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
% Assign defaults in case we want to try to continue;
a = 1;
b = 2;
end
Actually, in my code I make it even more robust. I check if the structure has the field before trying to assign it. Let me know if you want that code.
Related Question