how to use load function??

92 Ansichten (letzte 30 Tage)
nayana
nayana am 18 Feb. 2013
Kommentiert: Walter Roberson am 22 Jun. 2022
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 .

Akzeptierte Antwort

Image Analyst
Image Analyst am 18 Feb. 2013
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.

Weitere Antworten (3)

Azzi Abdelmalek
Azzi Abdelmalek am 18 Feb. 2013
filename='yourfile'
load(filename)

Mark Whirdy
Mark Whirdy am 18 Feb. 2013
Bearbeitet: Mark Whirdy am 18 Feb. 2013
do you really want to use load with text files?
myfile = load([fileName,'.',fileExtension]); % load is for workspaces really
myfile = myfile.myfile;
  1 Kommentar
Walter Roberson
Walter Roberson am 22 Jun. 2022
load() of text files is one of the fastest ways to read text data.

Melden Sie sich an, um zu kommentieren.


Upputholla divya teja
Upputholla divya teja am 22 Jun. 2022
i want to load the sesor activity data(file name asset-v1_KPITedu+EDUTECIF1041+2019+type@asset+block@DATA123.xls).i am also tried but it shows error like error usig load.
  1 Kommentar
Walter Roberson
Walter Roberson am 22 Jun. 2022
filename = 'asset-v1_KPITedu+EDUTECIF1041+2019+type@asset+block@DATA123.xls';
data = readtable(filename);
load() is not defined for xls files.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by