Reputation: 313
In Matlab, I have a little function init()
placed inside "init.m"
that I use to load all the data I need when I start up matlab. Files loaded include .mat
files and a .png
file. To load the .png
file, another function is called, importfile(filename)
. When I place the two functions in seperated files, everything works great. However, when I place this second function inside the file "init.m"
, and I call init()
from the command line, only the png appears in my workspace variables. I know that the first function in a .m
file is the main function and additional functions are considered local functions.
Can anyone explain this behavior? I am used to C++ and it would be helpful to precisely understand how Matlab handles paths, workspaces and multiple functions inside files.
Here are the relevant functions:
function init()
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
load(files(i).name);
end
importfile('./K2.png');
end
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 06-Jan-2015 12:10:28
% Import the file
rawData1 = importdata(fileToRead1);
% For some simple files (such as a CSV or JPEG files), IMPORTDATA might
% return a simple array. If so, generate a structure so that the output
% matches that from the Import Wizard.
[~,name] = fileparts(fileToRead1);
newData1.(genvarname(name)) = rawData1; %#ok<DEPGENAM>
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
end
Upvotes: 0
Views: 1385
Reputation: 12214
load()
only loads data into your function workspace. When you had init()
on its own I'm assuming you had it as a script and not as a function.
e.g.
% Script
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
load(files(i).name);
end
importfile('./K2.png');
vs.
function init()
% Function
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
load(files(i).name);
end
importfile('./K2.png');
end
A script's workspace is the base MATLAB workspace, so it's functioning as intended. When changed to a function it loads your data into the function workspace and discards it when the funciton completes execution. The reason the image still loads correctly is because you're explicitly assigning it to the base workspace.
To get around this, you could change init()
to:
function init()
% Function
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
evalin('base', strcat('load(''', files(ii).name, ''')'));
end
importfile('./K2.png');
end
And keep importfile
as a local function, which produces what I believe you're expecting. I'm not a huge fan of the syntax and blindly assigning workspace variables but it achieves the goal.
Upvotes: 0
Reputation: 313
My apologies, the answer is contained in the (MATLAB autogenerated) code:
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
The code also failed if I place it inside a seperate function; only not placing it in a function at all worked, as matlab considers this environment local.
Upvotes: 0