Load mat files located path into a function

305 views (last 30 days)
Marina
Marina on 30 May 2013
Answered: Helen Victoria on 14 Dec 2018
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.

Answers (4)

Jan
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():
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.

Image Analyst
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.
  1 Comment
Marina
Marina on 12 Jun 2013
Edited: Jan on 12 Jun 2013
Hi, thank you for your answer. I've tried this option, without specifying the folder first, and it returns "Undefined variable "input" or function "input.mat". I am unsure on how to incorporate the folder-part on the input arguments of the function. If you have further advice regarding this I would appreciate it. For the sake of completion, find the actual code below:
function [output]=dataconv(input,src_sig)
%
% dataconv
%
% Convolves the data stored in the input matrix with the selected source
% signature.
%
% IMPORTANT:
% PRELOAD DATA IN WORKSPACE
%
% Variable description:
% - input = input matrix, preloaded in workspace, call without
% hyphens nor .mat extension
% - src__sig = selected source signature, already resampled and centered, preloaded in workspace, call without
% hyphens nor .mat. If .asc, there is no need to preload in workspace, replace
% src_filename=load('filename.asc'), i.e.: output=dataconv(data,load('filename.asc'))
%
%
% EXAMPLE:
% test=dataconv(src6p,PGSsig1);;
%%PARAMETERS
[nt,ntrc]=size(input);
%%FULL CONVOLUTION
output=zeros(nt,ntrc);
for i=1:ntrc;
output(:,i)=conv(input(:,i),src_sig,'same');
end
%%ZERO-OFFSET TRACE DISPLAY
input_trc=input(:,1);
output_trc=output(:,1);
dt=0.002;
t=(0:nt-1)*dt;
figure
subplot(3,1,1), plot(t,input_trc), grid on, xlim([0.4 2]); title('Green´s function data'), xlabel('Time [s]'); ylabel('Amplitude')
subplot(3,1,2), plot(src_sig), grid on; title('Source signature:Resampled and Time-Shifted'),xlabel('Time [s]');ylabel('Amplitude')
subplot(3,1,3), plot(t,output_trc), grid on, xlim([0.4 2]); title('Convolution'),xlabel('Time [s]');ylabel('Amplitude')
set(gcf,'NextPlot','add');
axes;
h = title(['Zero-Offset Trace']);
set(gca,'Visible','off');
set(h,'Visible','on','Position',[0.5 1.05 0]);
[EDITED, Jan, code formatted]

Sign in to comment.


Jonathan Sullivan
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):
load(input);
  4 Comments
Marina
Marina on 12 Jun 2013
Hi!, I am aware of the differences between a script and a function yet, the problem, or rather the subject of my question, is not in the output to the memory space but in the input, the function is not loading the input I am telling it to load, unless, it is already located in the workspace.
Iain
Iain on 12 Jun 2013
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.

Sign in to comment.


Helen Victoria
Helen Victoria on 14 Dec 2018
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.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!