从函数内部加载 .mat 文件
Load .mat file from inside function
在 Matlab 中,我有一个小函数 init()
放在 "init.m"
中,当我启动 matlab 时,我用它来加载我需要的所有数据。加载的文件包括 .mat
个文件和一个 .png
个文件。要加载 .png
文件,需要调用另一个函数 importfile(filename)
。当我将这两个函数放在单独的文件中时,一切都很好。但是,当我将第二个函数放在文件 "init.m"
中并从命令行调用 init()
时,只有 png 出现在我的工作区变量中。我知道 .m
文件中的第一个函数是主要函数,其他函数被认为是局部函数。
谁能解释一下这种行为?我习惯了 C++,如果能准确理解 Matlab 如何处理文件中的路径、工作区和多个函数,将会很有帮助。
相关函数如下:
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
抱歉,答案包含在(MATLAB 自动生成的)代码中:
% 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
如果我将代码放在一个单独的函数中,代码也会失败;只是根本没有将它放在一个函数中,因为 matlab 认为这个环境是本地的。
load()
仅将数据加载到函数工作区中。当您单独拥有 init()
时,我假设您将其作为 script and not as a function.
例如
% Script
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
load(files(i).name);
end
importfile('./K2.png');
对比
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
脚本的工作区是基础 MATLAB 工作区,因此它按预期运行。当更改为函数时,它将您的数据加载到函数工作区中,并在函数完成执行时将其丢弃。图像仍然正确加载的原因是因为您明确地将它分配给了基础工作区。
要解决这个问题,您可以将 init()
更改为:
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
并将 importfile
保留为本地函数,它会产生您所期望的结果。我不太喜欢语法和盲目分配工作区变量,但它实现了目标。
在 Matlab 中,我有一个小函数 init()
放在 "init.m"
中,当我启动 matlab 时,我用它来加载我需要的所有数据。加载的文件包括 .mat
个文件和一个 .png
个文件。要加载 .png
文件,需要调用另一个函数 importfile(filename)
。当我将这两个函数放在单独的文件中时,一切都很好。但是,当我将第二个函数放在文件 "init.m"
中并从命令行调用 init()
时,只有 png 出现在我的工作区变量中。我知道 .m
文件中的第一个函数是主要函数,其他函数被认为是局部函数。
谁能解释一下这种行为?我习惯了 C++,如果能准确理解 Matlab 如何处理文件中的路径、工作区和多个函数,将会很有帮助。
相关函数如下:
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
抱歉,答案包含在(MATLAB 自动生成的)代码中:
% 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
如果我将代码放在一个单独的函数中,代码也会失败;只是根本没有将它放在一个函数中,因为 matlab 认为这个环境是本地的。
load()
仅将数据加载到函数工作区中。当您单独拥有 init()
时,我假设您将其作为 script and not as a function.
例如
% Script
cd('~/thesis/data/');
files = dir('*.mat');
for i=1:length(files)
disp(files(i).name);
load(files(i).name);
end
importfile('./K2.png');
对比
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
脚本的工作区是基础 MATLAB 工作区,因此它按预期运行。当更改为函数时,它将您的数据加载到函数工作区中,并在函数完成执行时将其丢弃。图像仍然正确加载的原因是因为您明确地将它分配给了基础工作区。
要解决这个问题,您可以将 init()
更改为:
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
并将 importfile
保留为本地函数,它会产生您所期望的结果。我不太喜欢语法和盲目分配工作区变量,但它实现了目标。