Undefined function/variable with function

1 次查看(过去 30 天)
In my main script, I have the following:
addpath(genpath('...'));
control_dir = '..'
patient_dir = '..'
% function
load_data(control_dir, patient_dir);
in my load_data function:
function [c_3darray, p_3darray] = load_data(control_dir, patient_dir)
controls = fullfile(control_dir, '*.mat');
patients = fullfile(patient_dir,'*.mat');
control_files = dir(controls);
patient_files = dir(patients);
num_roi = 379;
control_3darray = zeros(num_roi, num_roi, length(control_files));
patient_3darray = zeros(num_roi, num_roi, length(patient_files));
for i = 1:length(control_files)
origFilename = control_files(i).name;
fullFilename = fullfile(control_dir, origFilename);
% fprintf(1, '\nProcessing: %s\n', fullFilename);
[filepath, name, ext] = fileparts(fullFilename);
load(fullFilename);
c_3darray(:, :, i) = cov(x);
end
for i = 1:length(patient_files)
origFilename = patient_files(i).name;
fullFilename = fullfile(patient_dir, origFilename);
% fprintf(1, '\nProcessing: %s\n', fullFilename);
[filepath, name, ext] = fileparts(fullFilename);
load(fullFilename);
cov_mat = cov(fullmat_720tr_sc);
p_3darray(:, :, i) = cov(x);
end
end
when I run my main script, I get the error: Undefined function or variable 'control_3darray'. What am I missing here?

采纳的回答

Adam Danz
Adam Danz 2021-4-6
Given the incomplete error message, it seems that the error is not with the "control_3darray" variable but with x
c_3darray(:, :, i) = cov(x);
In the line above, x is not defined.
Also, c_3darray should probably be control_3darray. Same with p_3darray and patient_3darrayM.
There may be other errors as well but that's all the deeper I looked.
  2 个评论
Anita Sinha
Anita Sinha 2021-4-6
Yes, I changed some variables before posting the code here. I realized that in my main script, I should have called the function as
[c_3darray, p_3darray] = load_data(control_dir, patient_dir);
Adam Danz
Adam Danz 2021-4-6
The output variable names aren't the problem, though.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by