Problem calling multiple files through for-loop

3 views (last 30 days)
This is the for-loop function, named spike_analysis_wrapper:
function spike_analysis_wrapper()
filepairs = {
'spike1.mat', 'footsignal_spike1.mat';
'spike2.mat', 'footsignal_spike2.mat'};
% Loop through each row of filepairs
for i = 1:size(filepairs)
% Send the 2 files in row "i" into the analysis
spike_analysis(filepairs{i,1},filepairs{i,2});
end
end
And this is the main function, named spike_analysis:
function spike_analysis (file1,file2)
load(file1)
%Plot the spike with a corrected baseline
A=spike1(:,1);
B=spike1(:,2);
B1=B-(min(B)); %Baseline is 0 if the minimum value is diferent than 0.
figure(1)
plot(A,B1)
ylabel('Intensity (pA)')
xlabel('Time(s)')
title('1st spike')
% The paramenters of the foot signal
load(file2)
A_f=footsignal_spike1(:,1);
B_f=footsignal_spike1(:,2);
figure (2)
plot(A_f,B_f)
ylabel('Intensity (pA)')
xlabel('Time(s)')
title('1st foot signal')
end
The for-loop execute the first set of files (spike1 & footsignal_spike1) but I got this error when the loop tries to execute the second set of files (spike2 & footsignal_spike2).
Undefined function or variable 'spike1'.
Error in spike_analysis (line 5)
A=spike1(:,1);
Error in spike_analysis_wrapper (line 8)
spike_analysis(filepairs{i,1},filepairs{i,2});
According to the error, the problem lies on the main function (spike_analysis) because of the variable spike1.
How can I fix this problem?
Thanks in advance!

Accepted Answer

Stephen23
Stephen23 on 1 Sep 2019
Edited: Stephen23 on 3 Sep 2019
The problem is caused by badly designed .mat files, where every .mat file contains variables with different names. Your code assumes that the second files contains variables of the same name/s as the first file, but clearly this is not the case.Parsing multiple .mat files in a loop is much simpler when every .mat file contains exactly the same variable names.
If your .mat files contain exactly one variable per file, then you can use struct2cell:
S = load(file1);
C = struct2cell(S);
A = C{1}(:,1);
B = C{1}(:,2);
And the same for file2.
If your .mat files contain multiple variables per file, then you can load into a structure and then use dynamic fieldnames:
  10 Comments
Jose Rego Terol
Jose Rego Terol on 8 Sep 2019
Hi Stephen,
I have a question: How can I save a table for all files loaded in the loop's script that contains all the outputs of the function for each file loaded in one .xlsx file?
At the beginning I created a table containing the five outputs of the function in the function's script and then save the table in a .xlsx file. Obviusly, the file was overwriting everytime by the loop. Then, I include the table inside the loop, but the table only contains the first output of the function of the last file loaded.
Actually, the value of the function in the loop is only the first output of the function. It is abnormal because the function has 5 output and the loop should give all the outputs, right?
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', baseFileName)
%Call the function Spike_analysis
spike_analysis_file1(fullFileName)
end
function [T,Q_spike,I_max,halfMax,AT_HW,AT_RT] = spike_analysis_file1 (file1)
S = load(file1);
C = struct2cell(S);
A = C{1}(:,1);
B = C{1}(:,2);
%Save the parameters and the entire spike figure
Table_parameters_spike=table(T,Q_spike,I_max,AT_HW,AT_RT)
end
Now reading spike5.mat
Table_parameters_spike =
1×5 table
T Q_spike I_max AT_HW AT_RT
_______ ___________ ______ ________________ ________________
4.04856 368.0273714 79.834 920.000000000698 240.000000000684
ans =
4.04856
The ans of the loop is always the first output of the function (also the first column of the table).
in other words, how can I save the tables (in this case 5 tables, one per file) in one .xlsx file?
Stephen23
Stephen23 on 8 Sep 2019
Edited: Stephen23 on 8 Sep 2019
"How can I save a table for all files loaded in the loop's script that contains all the outputs of the function for each file loaded in one .xlsx file?"
Using writetable or xlswrite, both of which let you specify the destination worksheet and/or range to save the data to (thus easily avoiding overwriting data).
"Obviusly, the file was overwriting everytime by the loop."
You forgot to write the new data to different sheets and/or ranges, to ensure that the data was not overwritten.
"...but the table only contains the first output of the function of the last file loaded."
In fact what your code shows is that you did not call your function with any output arguments at all (something that has been thoroughly discussed in previous comments). The table Table_parameters_spike is being displayed in the command window, and you have not defined it as an output of your function. You appear to be confusing some data being displayed in the command window with the output arguments of your function (none of which appear to be tables).
"Actually, the value of the function in the loop is only the first output of the function."
Because you did not allocate the function outputs to any variables, MATLAB allocates the first ouput argument to the default ans:
"It is abnormal because the function has 5 output and the loop should give all the outputs, right?"
Nope, not abnormal at all... in fact because you did not allocate any of the outputs to variables, I would not expect "all the outputs", I would expect only the first one allocated to the default ans.
Very basic MATLAB concepts, such as how to call functions with multiple output arguments, are explained in the introductory tutorials:
You need to decide if you want to:
  1. return the table from the function (in which case it needs to be an output argument AND you need to call the function with an appropriate number of output arguments AND you need to decide if you also save the table inside the loop (see point 2) or somehow join/concatenate the data into one table and save it after the loop)
  2. save the table inside the loop (in which case you need to decide what destination worksheet/range to use to prevent data from being overwritten).
So what would you prefer?

Sign in to comment.

More Answers (0)

Categories

Find more on Timetables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!