Load mat files located path into a function

338 Ansichten (letzte 30 Tage)
Marina am 30 Mai 2013
Beantwortet: Helen Victoria am 14 Dez. 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:
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.

Antworten (4)

Jan am 12 Jun. 2013
Bearbeitet: Jan am 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:
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
Image Analyst am 30 Mai 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 Kommentar
Marina am 12 Jun. 2013
Bearbeitet: Jan am 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.
% 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'))
% test=dataconv(src6p,PGSsig1);;
for i=1:ntrc;
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')
h = title(['Zero-Offset Trace']);
set(h,'Visible','on','Position',[0.5 1.05 0]);
[EDITED, Jan, code formatted]

Melden Sie sich an, um zu kommentieren.

Jonathan Sullivan
Jonathan Sullivan am 30 Mai 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):
  4 Kommentare
Marina am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Helen Victoria
Helen Victoria am 14 Dez. 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.


Mehr zu Get Started with MATLAB 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