Load mat files located path into a function
169 次查看(过去 30 天)
显示 更早的评论
Hello!, I have a function output=function(input), where 'input' is a mat file that is saved on my path and to which I want to apply the operation defined by 'function'
So far, I haven't been able to make the function call the file from the path and have been force to load it into the workspace prior to calling the function.
For the function code itself I have:
[output]=function(input);
data=load(input);
The problem is that this kind of syntax loads the mat files as structured arrays and I need them to be load as the simple mat files they are.
I am new in Matlab so maybe I am probably just making a silly mistake so, I will greatly appreciate any assistance.
0 个评论
回答(4 个)
Jan
2013-6-12
编辑:Jan
2013-6-12
Please post the line, which causes the error. It is confusing that you use "input" as name of a variable and obviously as contents of this variable at the same time. In addition "input" is a built-in Matlab function.
I guess, the proble is related to the frequently occurring problems with the functional form of load():
load a
is equivalent to:
load('a')
but not to:
load(a)
Perhaps this helps:
FileName = 'input.mat';
FolderName = 'C:\Temp';
File = fullfile(FolderName, FileName);
load(File); % not: load('File')
I frequently suggest not to load variables directly to the workspace, because one can never know if a local variable is overwritten:
k = 10;
load('YourFile.mat');
disp(k)
Reading the source code cannot reveal the final value of k, because it depends on the contents of the MAT file. Therefore it is much saver to store the output in a struct. In addition this is faster also, because Matlab's JIT accelerator cannot be confused by dynamically redefined variables.
0 个评论
Image Analyst
2013-5-30
I would recommend explicitly putting the folder in there rather than have it search the path to find it, which can be a problem if there are two folders with that filename.
fullFileName = fullfile(folder, 'whatever.mat');
The mat file will load as one structure, not multiple. You can extract the individual variables
storedStructure = load(fullFileName);
myString = storedStructure.myString;
myDouble = storedStructure.myDouble;
Of course if you have a lot of them it makes your output argument list very long and I'd recommend just passing the whole structure back so that everything is contained in one convenient variable. In your calling routine you can then refer to the structure members (like on the right side of the lines above), or you can extract them into separate variables, like the lines above did.
Jonathan Sullivan
2013-5-30
Not sure if I understand your problem, but it sounds like you want the data in your mat file loaded as variables in the workspace, not as fields of a structure. If so, just try replacing your load line with this (notice no output to the load command):
load(input);
4 个评论
Iain
2013-6-12
put a breakpoint immediately before and after the load(input) line, and look at the workspace of the function in both cases. I suspect that you will find there are new variables which are the variables you ought to be using.
Helen Victoria
2018-12-14
hello all,
I am beginner in deep learning.I have downloaded CIFAR10 dataset which has 5 batch file and one test batch file.how to make this as image datastore input as training and testing data.kindly help.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!