Load mat files located path into a function
463 views (last 30 days)
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:
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.
Jan on 12 Jun 2013
Edited: Jan on 12 Jun 2013
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():
is equivalent to:
but not to:
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;
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.
Image Analyst on 30 May 2013
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 on 30 May 2013
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):
Helen Victoria on 14 Dec 2018
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.