Undefined function/variable with function

8 Ansichten (letzte 30 Tage)
Anita Sinha
Anita Sinha am 6 Apr. 2021
Kommentiert: Adam Danz am 6 Apr. 2021
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?

Akzeptierte Antwort

Adam Danz
Adam Danz am 6 Apr. 2021
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 Kommentare
Anita Sinha
Anita Sinha am 6 Apr. 2021
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 am 6 Apr. 2021
The output variable names aren't the problem, though.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by