1

I have a folder A that contains folders B and C

A--B
   C--|
     --|mat file  

at the folder level, I have a startup scrit and I want from this script to load data available in data.mat file available in C1 folder.

so from my script A_script.m , I did :

load('C/C1/data.mat');

content of script file :

function data_startup
%WHC_PROJECT_STARTUP
bdclose all;
load('B\C\data_v2.0.mat');

but this does nothing,data not loaded and no error raised ! can someone help me ?

thanks

Shai
  • 111,146
  • 38
  • 238
  • 371
lola
  • 5,649
  • 11
  • 49
  • 61

3 Answers3

3

The reason is because a function introduces its own variable scope.1 The variables from the .mat file will be loaded into the scope of the function, but not into the global scope.


1. See also http://www.mathworks.co.uk/help/techdoc/matlab_prog/f0-38052.html#f0-38463.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

What I understand from the question is you have a data stored in a .mat-file in a sub-folder and you want to use it for a some kind of initialization. If you expect to use them later from the base workspace, then one possibility would be to change the function to a script:

%WHC_PROJECT_STARTUP
bdclose all;
load(fullfile('B', 'C', 'data_v2.0.mat'));

I would recommend here the use of the function

fullfile('B', 'C', 'data_v2.0.mat')

because this makes you code platform-independent (Linux uses '/', Windows '\'). If you want the content of the .mat-file loaded in you base workspace, just save the code above as script and execute it.

If you insist to read the file in an function and use it later in base workspace, then look at the following code

function data_startup()
%WHC_PROJECT_STARTUP
bdclose all;
temp_data=load(fullfile('B', 'C', 'data_v2.0.mat')); % will be loaded as structure
file_variables=fieldnames(temp_data);% get the field names as cell array
for ii=1:length(file_variables)
   % file_variables{ii} - string of the field name
   % temp_data.(file_variables{ii}) - dynamic field reference
   assignin('base', file_variables{ii}, temp_data.(file_variables{ii}));
end

The code should work, right now I am at home and can not test it, sorry.

I would prefer the script solution, assigning variables from one workspace to another could lead to problems with the support and the extension of the code (suddenly variables are created and you do not see where they come from). Here are some more examples how to access fields of a structure dynamically.

Ivan Angelov
  • 353
  • 1
  • 5
  • Hello Ivan, I would like to clear variable from workspace , I've tried evalin('base','clear var') but this is not working , any idea ? – lola Nov 09 '12 at 14:56
  • Hi, @lola! `evalin('base', 'clear(''var'');')` should work. I should warn you, that it is generally a bad idea to change one workspace from another - the code is getting so harder to maintain and debug! – Ivan Angelov Nov 23 '12 at 08:32
0

You can modify your function to have an output like this, then in your parent(caller) function you can use the data in this output variable

function output=data_startup
%WHC_PROJECT_STARTUP
bdclose all;
output=load('B\C\data_v2.0.mat');
zamazalotta
  • 423
  • 1
  • 6
  • 11
  • Here is some attention required. The function will return in this way a structure with the contents of **data_v2.mat**. It is not the same as executing `load('C/C1/data.mat');` and trying to access the variables of the .mat file in the workspace. – Ivan Angelov Feb 23 '12 at 19:40
  • You are right, if you want them in your workspace and not in a structure you can loop over the fieldnames of the structure with eval and get them out `eval([fieldname{i} '= struct.' fieldname{i} ])` – zamazalotta Feb 23 '12 at 19:47